{include}

    {include}标记用来在当前模板中包含其它模板。任何当前模板中可用的变量在被包含的模板中也同样可用。

属性名 类型 必需? 缺省 描述
filestringYesn/a 要包含的模板文件的名称
assignstringNon/a 被包含模板输出所要赋值的变量
[var ...][var type]Non/a 传递给模板的局部变量

例子7-17. 简单的{include}例子

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

{* 主体模板在此,$tpl_name变量将被一个值代替,如'contact.tpl'
*}
{include file="$tpl_name.tpl"}

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

例子7-18. {include}传递变量

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

    上面的模板要包含下面的links.tpl

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

例子7-19. {include}及赋值给一个变量

    本例中将nav.tpl的内容赋值给$navbar变量,然后在页面的顶和末显示。

<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>

例子7-20. 各种{include}资源演示

{* 绝对路径 *}
{include file='/usr/local/include/templates/header.tpl'}

{* 绝对路径(和上例一样)*}
{include file='file:/usr/local/include/templates/header.tpl'}

{* Windows绝对路径(必须使用"file:"前缀)*}
{include file='file:C:/www/pub/templates/header.tpl'}

{* 从命名为"db"的模板资源中包含 *}
{include file='db:header.tpl'}

{* 包含一个变量模板,例如$module = 'contacts' *}
{include file="$module.tpl"}

{* 无法取得预想效果,因为单引号不会有变量替代 *}
{include file='$module.tpl'}

{* 包含一个多变量模板,例如amber/links.view.tpl *}
{include file="$style_dir/$module.$view.tpl"}

    参见{include_php}{insert}{php}模板资源以及组件化的模板