Embedding Templates within Other Templates
Any template can be embedded within another template using the "embed" variable:
{embed="template_group/template"}
Where "template_group" is the name of the group and "template" is the name of the template. For example:
{embed="site/header"}
You must include both the template group and the template name in the embedded tag.
By default, you cannot use embeds inside of the Forum Module or in the Membership Templates.
Embed Variables for the Embedded Template
In the {embed=""} tag you can also specify parameters that will be used as variables in the embedded page. For example, if you specifiy a parameter of dog_name="Shadow":
{embed="site/header" dog_name="Shadow"}
Then in the site/header template, you can have a variable called {embed:dog_name}, which will be replaced with the value of "Shadow":
<h2>My dog's name is {embed:dog_name}</h2>
Since {embed=""} tags are processed after all of the other tags of the original template are processed, this means you can set these parameters with other variables too:
{exp:weblog:entries weblog="default_site" limit="1"}
{embed="site/export" the_id="{entry_id}"}
{/exp:weblog:entries}
You may also use embed variables in conditionals:
{if embed:dog_name == "Shadow"}
My dog's name is {embed:dog_name}.
{if:else}
{embed:dog_name} will never be as cool as Shadow.
{/if}
Notes
- You can nest embedded templates as deeply as you want. In other words, you can put a template within another template, within yet another one. However: You can not put a template within itself or you'll cause a run-away loop. You also can not put an "upstream" template inside of a "downstream" one. For example: You have two templates: "outer" and "content". You put "content" inside of "outer". You therefore can not put "outer" inside of "content".
- PHP settings for each Template are honored on a per-Template basis, which means that you can embed a Template that has PHP enabled into another Template which does not have PHP enabled.
- A template is fully rendered before any embedded templates are processed and included, which means you cannot break up tags between different templates and embed them in pieces.
- If you are using the Multiple Site Manager, you can embed templates from one site into another site. Details can be found here: Multiple Site Manager Variables and Parameters
User Contributed Notes
You can use the Fresh Variables module to use variables across multiple sites in Multiple Site Manager.
The above Notes...
references template tags. HTML tags can be split between embedded templates. i.e.<body> can be in site/.head while </body> is in site/.foot
When working with Multiple Site Manager and embeds that call from other site’s templates:
1. Variables do not transfer through sites, so if you want to have global variables they have to be defined in each MSM because even though you are embedding a template from another site, it is looking for the local site Global Variables, either that or define them each in your path.php file (but still they must match between sites).
2. Relative Pathing - depending upon how your server is setup you really have to think through your relative pathing (this is not an EE thing, but rather a code thing). This even means how to you path elements in your CSS.
You can also use the embed variables in the conditionals as a qualifier.
{if embed:dog_name}This will display if dog_name is set.
{/if}
You must have an ExpressionEngine license and have attained a forum rank of "Lab Assistant" (100 posts) to contribute notes to the User Guide