{include}

{include} tags are used for including other templates in the current template. Any variables available in the current template are also available within the included template.

Attribute NameTypeRequiredDefaultDescription
filestringYesn/aThe name of the template file to include
assignstringNon/aThe name of the variable that the output of include will be assigned to
[var ...][var type]Non/avariable to pass local to template

Example 7-17. Simple {include} example

<html>
<head>
  <title>{$title}</title>
</head>
<body>
{include file='page_header.tpl'}

{* body of template goes here, the $tpl_name variable
   is replaced with a value eg 'contact.tpl'
*}
{include file="$tpl_name.tpl"}

{include file='page_footer.tpl'}
</body>
</html>

Example 7-18. {include} passing variables

{include file='links.tpl' title='Newest links' links=$link_array}
{* body of template goes here *}
{include file='footer.tpl' foo='bar'}

The template above includes the example links.tpl below

<div id="box">
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff  ...
</foreach}
</ul>
</div>

Example 7-19. {include} and assign to variable

This example assigns the contents of nav.tpl to the $navbar variable, which is then output at both the top and bottom of the page.

<body>
  {include file='nav.tpl' assign=navbar}
  {include file='header.tpl' title='Smarty is cool'}
    {$navbar}
    {* body of template goes here *}
    {$navbar}
  {include file='footer.tpl'}
</body>

Example 7-20. Various {include} resource examples

{* absolute filepath *}
{include file='/usr/local/include/templates/header.tpl'}

{* absolute filepath (same thing) *}
{include file='file:/usr/local/include/templates/header.tpl'}

{* windows absolute filepath (MUST use "file:" prefix) *}
{include file='file:C:/www/pub/templates/header.tpl'}

{* include from template resource named "db" *}
{include file='db:header.tpl'}

{* include a $variable template - eg $module = 'contacts' *}
{include file="$module.tpl"}

{* wont work as its single quotes ie no variable substitution *}
{include file='$module.tpl'}

{* include a multi $variable template - eg amber/links.view.tpl *}
{include file="$style_dir/$module.$view.tpl"}

See also {include_php}, {insert}, {php}, template resources and componentized templates.