{include}

{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. The {include} tag must have the attribute "file", which contains the template resource path.

You can optionally pass the 'assign' attribute, which will specify a template variable name that the output of {include} will be assigned to instead of displayed.

All assigned variables' values are restored after the scope of the included template is left. This means you can use all variables from the including template inside the included template. But changes to variables inside the included template are not visible inside the including template after the {include} statement.

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-9. function {include}

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

{* body of template goes here *}
{include file="$tpl_name.tpl"} <-- will replace $tpl_name with value

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

You can also pass variables to included templates as attributes. Any variables explicitly passed to an included template as attributes are only available within the scope of the included file. Attribute variables override current template variables, in the case they are named alike.

Example 7-10. {include} passing variables

{include file='header.tpl' title='Main Menu' table_bgcolor='#c0c0c0'}

{* body of template goes here *}

{include file='footer.tpl' logo='http://my.example.com/logo.gif'}

where header.tpl could be

<table border='1' width='100%' bgcolor='{$table_bgcolor|default:"#0000FF"}'>
  <tr><td>
    <h1>{$title}</h1>
  </td></tr>
</table>

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

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

<body>
{include file='nav.tpl' assign=navbar}
{include file='header.tpl' title='Main Menu' table_bgcolor='#effeef'}
{$navbar}

{* body of template goes here *}

{include file='footer.tpl' logo='http://my.example.com/logo.gif'}
{$navbar}
</body>

Use the syntax for template resources to include files outside of the $template_dir directory.

Example 7-12. {include} template 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'}

See also {include_php}, {php}, Template Resources and Componentized Templates.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.