I have created some error dialogs using template partials but while the documentation said they can also be used for dynamic content vs template variables that are only for fixed content, I cannot seem to see how they work.
Example, I would like to pass a custom message to the template partial similar to how you can do with regular template as such, that way I can just have one and pass the proper error instead of creating many versions:
This works with regular templates:
{embed="account/.templateName message="My Custom Message"}
This does not seem to work with template partials?
I render template partials just like this:
{error-dialog}
on a regular template.
But the same does not work:
{error-dialog message="My Custom Message"}
The documentation does say they can hold dynamic content, but I don’t think they do, like regular templates. At least not sure how to pass them.
vw000,
Template partials cannot have variables passed to them like an embed can.
So {embed="account/.templateName message="My Custom Message"}
would work for an embed, {error-dialog message="My Custom Message"}
does not work for partials, which I think is what you’re experiencing.
The dynamic content that the documentation refers is the same type of dynamic content that a template would have. Partials are really just small chunks of a template to be reused over and over.
For example {title}
is a piece of “dynamic content”, along with other fields you may have created. Or a full channel entries loop is another piece of dynamic content.
So you might have different types of content blocks on your site. You don’t want to rewrite that code in every template you might need it on, so you create a partial.
When the template is parsed, unlike embeds which are parsed completely on their own, the code from a partial is essentially used to replace the tag in the template that calls it, then the template is parsed. Much like includes in PHP.
So if you have a template:
{exp:channel:entries ...}
{my_header_partial}
{/exp:channel:entries}
And a partial names my_header_partial
:
<h1>{title}</h1>
Then when the template is parsed, the template parser sees the template as below and then parses the template.
{exp:channel:entries ...}
<h1>{title}</h1>
{/exp:channel:entries}
As you can see, that partial can’t be used on its own as there’s no context for {title}
outside of being included in the template.
hope that helps.
Thanks, Andy, I’m actually using the partials as you stated, to avoid having to repeat the same code over and over again, like includes, that way you can just change the design in one piece of code, and it gets updated on the whole site vs updating a hundred different templates.
This is why I was using it for alert boxes, like success messages or errors, menus and other things that are repeated over and over again.
I will try to see how I can dynamically change the alert text depending on the error, or just create a group with templates instead of partials if nothing else works.
I probably will use embeds in this specific scenario.
The thing is I’m already using embeds inside this template that then use other embeds from PHP (depends on output), if possible I try to avoid nested embeds for performance reasons and since I’m not sure at what level they would break.
And usually I use embeds for larger content, for example a full header or footer, small pieces of code like just an alert box that has only 4 lines, I created them as partials instead of having a full template file for each little piece of code. But I guess in this case, embeds is the only way to go if I want to pass them dynamic text that I can change for each embed.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.