Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

{block}

{block}可在模板上定义一块区域,以进行模板继承。详细参见模板继承.

子模板中的{block}区域代码,将会替换父模板对应的区域代码。

另外,{block}可以设置成合并父子模板的相应区域。在子模板的{block}中定义 appendprepend,可以使子模板附加在父模板 {block}区域的后面或前面。 在{block}内容中使用{$smarty.block.parent},可以让父模板的区域代码放到 子模板{block}内的任何位置。

{blocks}可以嵌套使用。

属性:

参数名称 类型 必选参数 默认值 说明
name string Yes n/a 模板区域的名称

可选属性 (仅在子模板中使用):

名称 说明
append {block}区域代码将附加到父模板的{block}内容之后
prepend {block}区域代码将附加到父模板的{block}内容之前
hide 在没有该名称区域的时候,忽略区域内容。
nocache 关闭{block} 缓存

Example 7.15. 简单的 {block} 例子

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}

  

结果输出:


<html>
  <head>
    <title>Page Title</title>
  </head>
</html>


Example 7.16. 前面附加 {block} 例子

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}

  

结果输出


<html>
  <head>
    <title>Title - Page Title</title>
  </head>
</html>


Example 7.17. 后面附加 {block} 例子

parent.tpl


<html>
  <head>
    <title>{block name="title"} is my title{/block}</title>
  </head>
</html>

  

child.tpl


{extends file="parent.tpl"} 
{block name="title" append}
Page Title
{/block}

  

结果输出:


<html>
  <head>
    <title>Page title is my titel</title>
  </head>
</html>


Example 7.18. {$smarty.block.child} 例子

parent.tpl


<html>
  <head>
    <title>{block name="title"}The {$smarty.block.child} was inserted here{/block}</title>
  </head>
</html>

  

child.tpl


{extends file="parent.tpl"} 
{block name="title"}
Child Title
{/block}

  

结果输出:


<html>
  <head>
    <title>The Child Title was inserted here</title>
  </head>
</html>


Example 7.19. {$smarty.block.parent} 例子

parent.tpl


<html>
  <head>
    <title>{block name="title"}Parent Title{/block}</title>
  </head>
</html>

  

child.tpl


{extends file="parent.tpl"} 
{block name="title"}
You will see now - {$smarty.block.parent} - here
{/block}

  

结果输出:


<html>
  <head>
    <title>You will see now - Parent Title - here</title>
  </head>
</html>


参见 模板继承, $smarty.block.parent, $smarty.block.child, 和 {extends}

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors