<table summary="Header navigation 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="language.function.assign.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.內置函數]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.call.html" accesskey="N">Next</a></td></tr></table>
# {block} 塊
{block} is used to define a named area of template source for template inheritance. For details see section of Template Interitance.
The {block} template source area of a child template will replace the correponding areas in the parent template(s).
Optionally {block} areas of child and parent templates can be merged into each other. You can append or prepend the parent {block} content by using the append or prepend option flag with the childs {block} definition. With the {$smarty.block.parent} the {block} content of the parent template can be inserted at any location of the child {block} content. {$smarty.block.child} inserts the {block} content of the child template at any location of the parent {block}.
{blocks}'s can be nested.
{block}用來定義一個命名的模板繼承源區域。具體細節可參考[模板繼承](#)章節。
{block}的一個子模板源區將取代父模板中的相應區域。
任意的子、父模板{block}區域可以彼此結合。可以利用子{block}定義中的*append*、*prepend*選項標記追加或預置父{block}內容。使用{$smarty.block.parent}可將父模板的{block}內容插入至子{block}內容中的任何位置。使用{$smarty.block.child}可將子模板{block}內容插入至父{block}內容中的任何位置。
{block}可以嵌套。
**Attribute: **
| Attribute Name | Type | Required | Default | Description |
|-----|-----|-----|-----|-----|
| name | string | yes | *n/a* | The name of the template source block |
**option flags(in children template only):**
| **Name** | **Description** |
|-----|-----|
| append | The {block} content will be be appended to the content of the parent template {block} |
| prepend | The {block} content will be prepended to the content of the parent template {block} |
| nocache | Disables caching of the {block} content |
**屬性: **
| 屬性名 | 類型 | 是否必須 | 缺省值 | 描述 |
|-----|-----|-----|-----|-----|
| name | string | yes | *n/a* | 模板源塊名稱 |
**選項標簽(只對子模版有效):**
| **名稱** | **描述** |
|-----|-----|
| append | {block}內容將追加到父模板{block}內容中 |
| prepend | {block}內容將添加到父模板{block}內容中 |
| nocache | 用'nocache' 屬性為變量賦值 |
譯注
| append與prepend作用方向是相反的,append是添加到父模板后面,而prepend是添加到父模板前面。 |
|-----|
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN2715" id="AEN2715"> </a> <b><span class="PROGRAMLISTING">Example 7.15. Simple {block} example</span><br/></b><strong>例 7-15.簡單的{block} 模板 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre>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}The result would look like<html> <head> <title>Page Title</title> </head></html></pre></td> </tr></table><p><b><span class="PROGRAMLISTING">Example 7.16. Prepend {block} example</span><br/></b> <strong>例 7-16.添加{block} 模板 </strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre>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} The result would look like<html> <head> <title>Title - Page Title</title> </head></html></pre> </td> </tr></table><p><b><span class="PROGRAMLISTING"> Example 7.17. Append {block} example</span><br/></b> <strong>例 7-17.追加{block} 模板</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> parent.tpl<br/> <html><br/> <head><br/> <title>{block name="title"} is my titel{/block}</title><br/> </head><br/> </html><br/><br/> child.tpl<br/> {extends file="parent.tpl"} <br/> {block name="title" append}<br/> Page Title<br/> {/block}<br/><br/> The result would look like<br/> <html><br/> <head><br/> <title>Page title is my titel</title><br/> </head><br/> </html></p> </td> </tr></table><p><b><span class="PROGRAMLISTING"> Example 7.18. {$smarty.block.child} example</span><br/>例 7-18.{$smarty.block.child}例子</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> parent.tpl<br/> <html><br/><head><br/><title>{block name="title"}The {$smarty.block.child} was inserted here{/block}<br/></head><br/></html><br/><br/>child.tpl<br/>{extends file="parent.tpl"} <br/>{block name="title" append}<br/>Child Title<br/>{/block}<br/><br/>The result would look like</p> <p><html><br/> <head><br/> <title>The - Child Title - was inserted here</title><br/> </head><br/> </html></p></td> </tr></table><p><b><span class="PROGRAMLISTING"> Example 7.19. {$smarty.block.parent} example</span><br/>例 7-19.{$smarty.block.parent}例子</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> parent.tpl<br/> <html><br/><head><br/><title>{block name="title"}Parent Title{/block}</title><br/></head><br/></html><br/><br/>child.tpl<br/>{extends file="parent.tpl"} <br/>{block name="title" append}<br/>You will see now - {$smarty.block.parent} - here<br/>{/block}<br/><br/>The result would look like</p> <p><html><br/> <head><br/> <title>You will see now - Parent Title - here</title><br/> </head><br/> </html><br/></p> </td> </tr></table><p>See also Template interitance, $smarty.block.parent, $smarty.block.child, and {extends}<br/>參考<a href="advanced.features.tem.inheritance.html">模板繼承</a>、<a href="language.variables.smarty.html#pblock">$smarty.block.parent</a>、 <a href="language.variables.smarty.html#cblock">$smarty.block.child</a>和<a href="language.function.extends.html">{extends}</a> </p> </div></td> </tr></table>
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.assign.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.call.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{assign}<br/> 賦值</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{call}<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使用指南
- 翻譯人員列表