<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/> <html><br/> <head><br/> <title>{block name=title}Default Page Title{/block}</title><br/> <span style="color: blue">{block name=head}{/block}</span><br/> </head><br/> <body><br/> {block name=body}{/block}<br/> </body><br/> </html></p> <p> <br/> myproject.tpl (child) 子模板 <br/> {extends file=layout.tpl}<br/> {block name=head}<br/> <link href="/css/mypage.css" rel="stylesheet" type="text/css"/><br/> <script src="/js/mypage.js"></script><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/> <link href="/css/mypage.css" rel="stylesheet" type="text/css"/><br/> <script src="/js/mypage.js"></script><br/> {/block}<br/> {block name=body}My HTML Page Body goes here{/block}</p> <p> <br/> To render the above use 上述使用如下渲染 <br/> $smarty->display('mypage.tpl');</p> <p><br/> The resulting output is 输出结果<br/> <html><br/> <head><br/> <title>My Page Title</title><br/> <link href="/css/mypage.css" rel="stylesheet" type="text/css"/><br/> <script src="/js/mypage.js"></script><br/> </head><br/> <body><br/></p> <p>My HTML Page Body goes here<br/> </body><br/> </html></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/> <?php<br/> $smarty->display('extends:layout.tpl|myproject.tpl|mypage.tpl'); <br/> ?></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>
- Smarty模板编译引擎
- 序
- 译序
- I.开始
- 第一章. 什么是Smarty?
- 第二章. 安装
- II.模板设计者篇
- 第三章.基本语法
- 注释
- 变量
- 函数
- 属性
- 双引号里嵌入变量
- 数学运算
- 忽略Smarty解析
- 第四章.变量
- 从PHP分配的变量
- 从配置文件读取的变量
- 变量范围
- {$smarty}保留变量
- 第五章.变量调节器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.组合修改器
- 第七章.内置函数
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定义函数
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.调试控制台
- III.模板程序员篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty类变量
- $template_dir
- 第十三章.Smarty类方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.缓存
- 建立缓存
- 多重缓存
- 缓存集合
- 控制插件输出的可缓存性
- 第十五章.高级特性
- 安全
- 通过模板更改设置
- 模板继承
- 数据流
- 对象
- 静态类
- 预过滤器
- 后过滤器
- 输出过滤器
- 缓存处理函数
- 资源
- 第十六章.以插件扩展Smarty
- 插件如何工作
- 命名约定
- 编写插件
- 模板函数
- 调节器
- 块函数
- 编译函数
- 预滤器/后滤器
- 输出过滤器
- 资源
- 插入
- Ⅳ.附录
- 第十七章.疑难解答
- Smarty/PHP 错误
- 第十八章.使用技巧和经验
- 空白变量处理
- 默认变量处理
- 传递变量标题给头模板
- 日期
- WAP/WML
- 组件化模板
- 拒绝电子邮件地址
- 第十九章. 相关资源
- 第二十章. 漏洞
- 3.0安装包
- 2.x版本升级至3.x版本的提示
- 3.0.x使用指南
- 翻译人员列表