<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&lt;html&gt; &lt;head&gt; &lt;title&gt;{block name="title"}Default Title{/block}&lt;/title&gt; &lt;title&gt;{block "title"}Default Title{/block}&lt;/title&gt; {* short-hand *} &lt;/head&gt;&lt;/html&gt; child.tpl{extends file="parent.tpl"} {block name="title"}Page Title{/block}The result would look like&lt;html&gt; &lt;head&gt; &lt;title&gt;Page Title&lt;/title&gt; &lt;/head&gt;&lt;/html&gt;</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&lt;html&gt; &lt;head&gt; &lt;title&gt;{block name="title"}Title - {/block}&lt;/title&gt; &lt;/head&gt;&lt;/html&gt; child.tpl{extends file="parent.tpl"} {block name="title" prepend}Page Title{/block} The result would look like&lt;html&gt; &lt;head&gt; &lt;title&gt;Title - Page Title&lt;/title&gt; &lt;/head&gt;&lt;/html&gt;</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/> &lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;{block name="title"} is my titel{/block}&lt;/title&gt;<br/> &lt;/head&gt;<br/> &lt;/html&gt;<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/> &lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;Page title is my titel&lt;/title&gt;<br/> &lt;/head&gt;<br/> &lt;/html&gt;</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/> &lt;html&gt;<br/>&lt;head&gt;<br/>&lt;title&gt;{block name="title"}The {$smarty.block.child} was inserted here{/block}<br/>&lt;/head&gt;<br/>&lt;/html&gt;<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>&lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;The - Child Title - was inserted here&lt;/title&gt;<br/> &lt;/head&gt;<br/> &lt;/html&gt;</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/> &lt;html&gt;<br/>&lt;head&gt;<br/>&lt;title&gt;{block name="title"}Parent Title{/block}&lt;/title&gt;<br/>&lt;/head&gt;<br/>&lt;/html&gt;<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>&lt;html&gt;<br/> &lt;head&gt;<br/> &lt;title&gt;You will see now - Parent Title - here&lt;/title&gt;<br/> &lt;/head&gt;<br/> &lt;/html&gt;<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>