Files
smarty/docs/en/designers/language-builtin-functions/language-function-include.xml
2005-06-05 19:46:19 +00:00

185 lines
4.9 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.function.include">
<title>{include}</title>
<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>
<para>
{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.
</para>
<para>
You can optionally pass the <emphasis>'assign'</emphasis> attribute,
which will specify a template variable name that the output of
{include} will be assigned to instead of
displayed.
</para>
<example>
<title>function {include}</title>
<programlisting>
<![CDATA[
<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>
]]>
</programlisting>
</example>
<para>
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.
</para>
<example>
<title>{include} passing variables</title>
<programlisting>
<![CDATA[
{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"}
]]>
</programlisting>
<para>where header.tpl could be</para>
<programlisting>
<![CDATA[
<table border='1' width='100%' bgcolor='{$table_bgcolor|default:"#0000FF"}'>
<tr><td>
<h1>{$title}</h1>
</td></tr>
</table>
]]>
</programlisting>
</example>
<example>
<title>{include} and assign to variable</title>
<para>This example assigns the contents of nav.tpl to the $navbar variable,
which is then output at the top and bottom of the page.
</para>
<programlisting>
<![CDATA[
<body>
{include file="nav.tpl" assign="navbar"}
{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}
{$navbar}
{* body of template goes here *}
{include file="footer.tpl" logo="http://my.example.com/logo.gif"}
{$navbar}
</body>
]]>
</programlisting>
</example>
<para>
Use the syntax for <link
linkend="template.resources">template resources</link> to
include files outside of the <link linkend="variable.template.dir">$template_dir</link>
directory.
</para>
<example>
<title>{include} template 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"}
]]>
</programlisting>
</example>
<para>
See also
<link linkend="language.function.include.php">{include_php}</link>,
<link linkend="language.function.php">{php}</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
-->