Okay guys.
So initially I've mentioned that we might want to use partial templates as as we can see inside the
list of the blog posts on the posts page, we've got this fragment.
Its job is to display the titles of blog posts together with an excerpt of the content and links to
the blog posts.
So this fragment is identical to the same fragment on the home page.
What we might want to do is to extract this into a partial template that we are going to render in a
couple other templates.
So let me grab this code and inside views create the folder for the partials.
So it's a.
So now we need to come up with some convention.
I think we can start the names with the underscores.
This means it is a partial.
Let me call that just Posts.
And this is our partial template.
Now, I think we should jump to the view class and create some kind of a method to render the partial.
Now inside the view class we immediately see the render template method, and I think that it wouldn't
be a very bad idea to reuse it for rendering of partials.
So it would be quite simple.
We will just add a static method called partial that might accept the data and template as the arguments,
initializing the data with an empty array and will return a string.
So how we would like to use the render template is.
We're gonna return the call to static render a template, but we're just going to assume that the template
is inside partials folder.
Then we pass the partial template name and the second parameter is the potential data.
Okay.
So let's jump to the home template.
And now let's try to output something using Vue partial.
The template name is posts.
And we need to pass the data.
And we have it right here.
This would be under sorry.
That key should be posts.
And we have the posts variable here.
So I think this should work.
Now there is one problem though.
If we use a class we need to add a use statement for it.
So we need to add something like this at the top of every stem of every template where we use this partial.
That's not ideal.
We're gonna fix this later on.
For now, let's just see if it works.
So the home template and this one, let's also make sure we import the view class in here.
And let's try that out.
So let's move to the browser.
And it seems that both pages are fine.
Let me also search for something.
Okay.
Everything is fine.
So now let's do something about having to import this class every single time.
So my suggestion is to create a file with helper functions.
So how can we do that.
Well it's up to us.
Where do we put this file.
It might be at the root directory.
Let's call it helpers PHP.
So we need to add a use statement for the core view, and our helper function will be called partial.
It will be globally available.
Thus we won't have to add a use statement for a class.
So that's a template and array data that is initialized as an empty array.
It returns the string.
And all it does is it does view partial passing the template and the data.
Now what's better is we can first make sure that such function doesn't exist.
We can use PHP function exists for that.
So if the partial function was already defined in the global scope, we don't define it.
If it wasn't, we can safely define it.
That's not everything we need to make sure this is available.
It only makes sense to make this helpers available inside the index.php, not inside the bootstrap,
because it is specific to the web part of our project.
Maybe we can add the require ones in here and just make sure we've got helpers.
PHP.
Now let's jump to our templates.
So this one first let's just call the partial function removing the use statement.
And the template for the post.
And also call the partial function removing the use statement.
Everything still works.
So we were fine.
Okay so this is how can you create and use partial templates?
I think it's a great feature and makes our code much more usable.