<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使用指南
- 翻譯人員列表