Files
smarty/docs/en/designers/language-builtin-functions/language-function-include.xml

218 lines
6.1 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.function.include">
<title>{include}</title>
<para>
<varname>{include}</varname> 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.
</para>
<itemizedlist>
<listitem><para>
The <varname>{include}</varname> tag must have
the <parameter>file</parameter> attribute
which contains the template resource path.
</para></listitem>
<listitem><para>
Setting the optional <parameter>assign</parameter> attribute
specifies the template variable that the output of
<varname>{include}</varname> is assigned to, instead of being displayed. Similar to
<link linkend="language.function.assign"><varname>{assign}</varname></link>.
</para></listitem>
<listitem><para>
Variables can be passed to included templates as
<link linkend="language.syntax.attributes">attributes</link>.
Any variables explicitly passed to an included template
are only available within the scope of the included
file. Attribute variables override current template variables, in
the case when they are named the same.
</para></listitem>
<listitem><para>
All assigned variable 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 <varname>{include}</varname> statement.
</para></listitem>
<listitem><para>
Use the syntax for <link
linkend="template.resources">template resources</link> to
<varname>{include}</varname> files outside of the
<link linkend="variable.template.dir"><parameter>$template_dir</parameter></link> directory.
</para></listitem>
</itemizedlist>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>file</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the template file to include</entry>
</row>
<row>
<entry>assign</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>The name of the variable that the output of
include will be assigned to</entry>
</row>
<row>
<entry>[var ...]</entry>
<entry>[var type]</entry>
<entry>No</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>variable to pass local to template</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<example>
<title>Simple {include} example</title>
<programlisting>
<![CDATA[
<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>
]]>
</programlisting>
</example>
<example>
<title>{include} passing variables</title>
<programlisting>
<![CDATA[
{include file='links.tpl' title='Newest links' links=$link_array}
{* body of template goes here *}
{include file='footer.tpl' foo='bar'}
]]>
</programlisting>
<para>The template above includes the example <filename>links.tpl</filename> below</para>
<programlisting>
<![CDATA[
<div id="box">
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff ...
</foreach}
</ul>
</div>
]]>
</programlisting>
</example>
<example>
<title>{include} and assign to variable</title>
<para>This example assigns the contents of <filename>nav.tpl</filename>
to the <varname>$navbar</varname> variable,
which is then output at both the top and bottom of the page.
</para>
<programlisting>
<![CDATA[
<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>
]]>
</programlisting>
</example>
<example>
<title>Various {include} resource examples</title>
<programlisting>
<![CDATA[
{* 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"}
]]>
</programlisting>
</example>
<para>
See also
<link linkend="language.function.include.php"><varname>{include_php}</varname></link>,
<link linkend="language.function.insert"><varname>{insert}</varname></link>,
<link linkend="language.function.php"><varname>{php}</varname></link>,
<link linkend="template.resources">template resources</link> and
<link linkend="tips.componentized.templates">componentized templates</link>.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->