向头模板传递变量标题

    如果你的大部分模板都使用相同的头和脚注,常用的方法是将这些内容分离出来成为其独立的模板,然后{include}它们。但是,如果头需要一个不同的标题,取决于你来自何处时,该如何做?你可以在包含头模板时将标题作为一个属性传递给头模板。

例子18-3. 传递标题变量给头模板

mainpage.tpl - 当显示主页面时,标题“Main Page”将传递给header.tpl,并用来作为标题。

{include file='header.tpl' title='Main Page'}
{* template body goes here *}
{include file='footer.tpl'}

archives.tpl - 当显示归档页面时,标题是“Archives”。注意在这个例子中,我们使用来自archives_page.conf文件中的变量而不是硬性指定的变量。

{config_load file='archive_page.conf'}

{include file='header.tpl' title=#archivePageTitle#}
{* template body goes here *}
{include file='footer.tpl'}

header.tpl - 注意,如果$title变量没有设置,那么会显示“Smarty News”(使用缺省变量修饰符)。

<html>
<head>
<title>{$title|default:'Smarty News'}</title>
</head>
<body>

footer.tpl

</body>
</html>