{extends}
模板继承中,你可以在子模板内使用{extends}标签来扩展父模板。 详细参见模板继承.
-
{extends}必须放在模板的第一行。 -
如果子模板要用
{extends}来扩展父模板,那么它只能有{block}的区域。任何其他的模板内容都会被忽略。 -
扩展使用在
$template_dir之外的文件,请使用模板资源的语法。
PS:当你扩展一个父模板名称的变量如{extends file=$parent_file}, 请确保$parent_file变量放到同一个 $cache_id中. 否则Smarty无法辨别不同的$parent_file变量。
简单 {extends} 例子
{extends file=‘parent.tpl‘}
{extends ‘parent.tpl‘} {* short-hand *}
{block}
{block}可在模板上定义一块区域,以进行模板继承。详细参见模板继承.
子模板中的{block}区域代码,将会替换父模板对应的区域代码。
另外,{block}可以设置成合并父子模板的相应区域。在子模板的{block}中定义 append 或 prepend,可以使子模板附加在父模板 {block}区域的后面或前面。 在{block}内容中使用{$smarty.block.parent},可以让父模板的区域代码放到 子模板{block}内的任何位置。
{blocks}可以嵌套使用。
属性:
| 参数名称 | 类型 | 必选参数 | 默认值 | 说明 |
|---|---|---|---|---|
| name | string | Yes | n/a | 模板区域的名称 |
可选属性 (仅在子模板中使用):
| 名称 | 说明 |
|---|---|
| append | {block}区域代码将附加到父模板的{block}内容之后 |
| prepend | {block}区域代码将附加到父模板的{block}内容之前 |
| hide | 在没有该名称区域的时候,忽略区域内容。 |
| nocache | 关闭{block} 缓存 |
简单的 {block} 例子
parent.tpl
<html>
<head>
<title>{block name="title"}Default Title{/block}</title>
<title>{block "title"}Default Title{/block}</title> {* short-hand *}
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title"}
Page Title
{/block}
结果输出:
<html>
<head>
<title>Page Title</title>
</head>
</html>
前面附加 {block} 例子
parent.tpl
<html>
<head>
<title>{block name="title"}Title - {/block}</title>
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title" prepend}
Page Title
{/block}
结果输出
<html>
<head>
<title>Title - Page Title</title>
</head>
</html>
后面附加 {block} 例子
parent.tpl
<html>
<head>
<title>{block name="title"} is my title{/block}</title>
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title" append}
Page Title
{/block}
结果输出:
<html>
<head>
<title>Page title is my titel</title>
</head>
</html>
{$smarty.block.child} 例子
parent.tpl
<html>
<head>
<title>{block name="title"}The {$smarty.block.child} was inserted here{/block}</title>
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title"}
Child Title
{/block}
结果输出:
<html>
<head>
<title>The Child Title was inserted here</title>
</head>
</html>
{$smarty.block.parent} 例子
parent.tpl
<html>
<head>
<title>{block name="title"}Parent Title{/block}</title>
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title"}
You will see now - {$smarty.block.parent} - here
{/block}
结果输出:
<html>
<head>
<title>You will see now - Parent Title - here</title>
</head>
</html>
{include}
{include}用于载入其他模板到当前模板中。 在包含模板中可用的变量,载入后在当前模板仍然可用。
-
{include}必须设置file属性,设置载入的文件资源路径。 -
设置了可选的
assign属性,将{include}模板的内容赋值到变量,而并非输出。 与{assign}操作相似。 -
包含模板时,可以像使用属性一样设置传递的变量。 这样传递的变量,作用范围仅限于包含的模板内。 属性传递的变量将覆盖原包含模板的同名变量。
-
你可以在当前模板内使用包含模板的全部变量。 但是如果包含模板内有修改或者新建变量,那么这些变量只有包含模板的作用范围,而不可以是当前
{include}模板中使用。 这种默认的设置,可以通过在包含模板时设置{include}的作用范围属性,或者 在修改或新增变量时通过{assign}的作用范围属性来设定。 后者在需要包含模板返回值时比较有用。 -
当文件不在
$template_dir目录中时, 使用资源的语法来{include}包含文件。
Attributes:
| 参数名称 | 类型 | 必选参数 | 默认值 | 说明 |
|---|---|---|---|---|
| file | string | Yes | n/a | 包含载入的文件名 |
| assign | string | No | n/a | 将包含的文件内容赋值给变量 |
| cache_lifetime | integer | No | n/a | 单独开启被包含模板的缓存时间 |
| compile_id | string/integer | No | n/a | 单独设置被包含模板的编译ID |
| cache_id | string/integer | No | n/a | 单独设置被包含模板的缓存ID |
| scope | string | No | n/a | 定义被包含模板的赋值变量作用范围: ‘parent‘,‘root‘ 或 ‘global‘ |
| [var ...] | [var type] | No | n/a | 传递到包含模板的变量 |
可选标记:
| 名称 | 说明 |
|---|---|
| nocache | 关闭包含模板的缓存 |
| caching | 打开包含模板的缓存 |
| inline | 设置成true时,在编译时把包含模板的内容也合并到当前模板的编译文件中。 |
Example 7.46. 简单 {include} 例子
<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"}
{* using shortform file attribute *}
{include ‘page_footer.tpl‘}
</body>
</html>
Example 7.47. {include} 传递变量
{include ‘links.tpl‘ title=‘Newest links‘ links=$link_array}
{* body of template goes here *}
{include ‘footer.tpl‘ foo=‘bar‘}
包含了下面的 links.tpl 模板
<div id="box">
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff ...
</foreach}
</ul>
</div>