Template Partials
You can include a template in other templates by using partials. Partials are a special syntax that use the template shortcut to reference other templates.
If you'd like to add your email signature at the end of several templates, you don’t have to copy-paste it in each of the templates. You can create a single signature
template, and use it as a partial in all the other templates.
{{> signature}}
This way, instead of inserting two templates (the main response and the signature), you'll only insert the main response that contains the signature partial.
Another advantage is that if you change the signature
template, it will change in all the other templates.
Basic partials
You can include a template in another template by using the special partial syntax and the other template's shortcut. The template shortcut is the same one you can use to insert that specific template with Tab .
{{> template_shortcut}}
This will include the template_shortcut
template into the current template.
Dynamic partials
You can use dynamic expressions like Variables or Helpers to generate dynamic partial names by using subexpressions.
For example, you can include a different partial depending on a variable value. You'll need to create a separate template for each possible value and use {{> (lookup . 'variable')}}
in the main template.
You can also include a different partial based on a helper value. For example, to include a different partial based on the AM/PM time of day, you can use {{> (moment format="a")}}
.
Partial context
Partials can access the same variables as any other template. If you only want to use certain variables in a partial, you can set a different context for it.
Keep in mind that if the variables used in a template depend on a custom context, those variables will not be available when you insert that template by itself.
Partial parameters
You can pass any variable to a partial, even custom variables, by using {{> partial variable=value}}
.
When partials are used inside an {{each}}
block helper, the context of the partial will be the current item. To work around this, you can pass the variables you need as parameters.
Partial blocks
When a partial is not found (because you do not have a template with that shortcut), an error will be displayed instead of the template. If you'd like to display different content when the partial is not found, and not show an error, you can use the block syntax.
{{#> signature }}
This will be displayed when you don't have a template with the "signature" shortcut.
{{/signature}}
You can use this same block syntax and a special @partial-block
partial to create layouts. For example, you can create a layout template that contains a header and footer, and use it in other templates that only contain the main content.
Inline partials
Instead of using other templates as partials, you can declare partials inside the current template. This can be useful if you find yourself repeating the same value or content in a single template.
You can combine the inline partial syntax with partial blocks to create advanced layouts with multiple content placeholders.