<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="advanced.features.changing.settings.by.tem.html">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 15. Advanced Features 高级特性</td><td width="25%" align="right" valign="bottom"> <a href="advanced.features.streams.html">Next</a></td></tr></table> # [Template Inheritance]()[模板继承] Inheritance brings the concept of Object Oriented Programming to templates, allowing you to define one (or more) base templates that can be extended by child templates. Extending means that the child template can override all or some of the parent named block areas. The inheritance tree can be as big as you want (meaning you can extend a file that extends another one that extends another one and so on..), but be aware that all files have to be checked for modifications at runtime so the more inheritance the more overhead you add. The child templates can not define any content besides what's inside {block} tags they override, anything outside of {block} tags will be removed. The content of {block} tags from child and parent templates can be merged by the append or prepend {block} tag option flags and {$smarty.block.parent} or {$smarty.block.child} placeholders. Template inheritance is a compile time process which does create a single compiled template file. Compared to corresponding solutions based on subtemplates included with the {include} tag it does have much better performance when redering. The child template does extend its parent defined with the {extends} tag, which must be the first line in the child template. Instead of using the {extends} tags in the template files you can define the whole template inheritance tree in the PHP script when you are calling fetch() or display() with the extends: template resource type. The later provides even more flexibillity. 继承带来了模板面向对象概念(oop),它允许你定义一个或多个基模板供子模板继承。继承意味着子模板可覆盖所有或部份父模板中命名相同的块区域。 继承树大小没有规定,只要你愿意你想搞多大都可以(意为你继承的文件上面有个父文件,父文件上面可以有个爷文件,爷文件上面有个曾祖父文件...生生不息无穷尽),但需要注意所有文件都必须在运行时检查修改设置,更多的继承意味着更大的开销。 子模板不能定义任何内容,除了需要覆盖父模板的[{block}](#)标签块,所有在{block}标签外的内容将被自动移除。 子模板和父模板的{block}标签内容可以合并,方法一:用***append***添加或***prepend***追加{block}标签选项标记;方法二:使用[{$smarty.block.parent}](#)或[{$smarty.block.child}](#)占位符。 模板继承是一种编译时进程,其将建立一个独立的编译模板文件。与对应的基于载入[{include}](#)子模板解决方案相比,当解释模板时,前者有更好的性能。 子模板通过[{extends}](#)标签定义继承父模板,该定义应写在子模板的第一行。与在模板文件中使用{extends}标签不同的是,你可以使用“*extends:*模板资源类型”(见[extends:resource](#)),调用[fetch()](#)或[display()](#)函数在php脚本中定义整个模板继承树。后一个方法提供更大的弹性。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> If you have a subtemplate which is included with {include} and it does contain {block} areas it does work only if the {include} itself is called from within a surrounding {block}.In the final parent template you may need a dummy {block} for it.<br/> 如果有一个通过{include}标签载入的子模板,而且它包含{block}区域,那么该{block}区域只能工作在调用{include}标签的{bolck}块的内部。在最终的模板中,你应该再为该{include}子模板添加另一个形式上的{block}标签。</td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <strong><a name="AEN4156" id="AEN4156"> </a>Example 15.6. Template inheritance example<br/> 例15-6.模板继承示例</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> layout.tpl (parent) 父模板 <br/> &lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;{block name=title}Default Page Title{/block}&lt;/title&gt;<br/> &lt;span style="color: blue"&gt;{block name=head}{/block}&lt;/span&gt;<br/> &lt;/head&gt;<br/> &lt;body&gt;<br/> {block name=body}{/block}<br/> &lt;/body&gt;<br/> &lt;/html&gt;</p> <p> <br/> myproject.tpl (child) 子模板 <br/> {extends file=layout.tpl}<br/> {block name=head}<br/> &lt;link href="/css/mypage.css" rel="stylesheet" type="text/css"/&gt;<br/> &lt;script src="/js/mypage.js"&gt;&lt;/script&gt;<br/> {/block}</p> <p> <br/> myproject.tpl (grandchild) 孙子模板(译注:应为mypage.tpl吧?!) <br/> {extends file=project.tpl}<br/> {block name=title}My Page Title{/block}<br/> {block name=head}<br/> &lt;link href="/css/mypage.css" rel="stylesheet" type="text/css"/&gt;<br/> &lt;script src="/js/mypage.js"&gt;&lt;/script&gt;<br/> {/block}<br/> {block name=body}My HTML Page Body goes here{/block}</p> <p> <br/> To render the above use 上述使用如下渲染 <br/> $smarty-&gt;display('mypage.tpl');</p> <p><br/> The resulting output is 输出结果<br/> &lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;My Page Title&lt;/title&gt;<br/> &lt;link href="/css/mypage.css" rel="stylesheet" type="text/css"/&gt;<br/> &lt;script src="/js/mypage.js"&gt;&lt;/script&gt;<br/> &lt;/head&gt;<br/> &lt;body&gt;<br/></p> <p>My HTML Page Body goes here<br/> &lt;/body&gt;<br/> &lt;/html&gt;</p></td></tr></table><p><strong>Example 15.7. Template inheritance by template resource extends:<br/>例15-7.通过</strong><strong>模板资源继承的模板继承</strong><br/>Instead of using {extends} tags in the template files you can define the inheritance tree in your PHP script by using the extends: resource type.<br/>The code below will return same result as the example above.<br/> 不在模板文件中使用{extends}标签,取而代之的是用“<em><strong>extends:</strong></em>模板资源类型”,在php脚本中定义整个模板继承树。下面的代码会返回与上例同样的结果。</p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><br/> &lt;?php<br/> $smarty-&gt;display('extends:layout.tpl|myproject.tpl|mypage.tpl'); <br/> ?&gt;</td> </tr></table><p> 参见<a href="language.function.block.html">{block}</a>、<a href="language.function.extends.html">{extends}</a>和<a href="template.resources.html#tidbps">extends: 资源</a>。</p> </div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="advanced.features.changing.settings.by.tem.html">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html">Home</a></td><td width="33%" align="right" valign="top"> <a href="advanced.features.streams.html">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Changing settings by template<br/> 通过模板更改设置</td><td width="34%" align="center" valign="top"><a href="smarty.for.programmers.html">Up</a></td><td width="33%" align="right" valign="top">Streams<br/> 数据流</td></tr></table>