<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="plugins.modifiers.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins</td><td width="25%" align="right" valign="bottom"><a href="plugins.compiler.functions.html" accesskey="N">Next</a></td></tr></table>
# Block Functions[块函数]
> void smarty_block_name($params, $content, &$smarty, &$repeat);
array $params;
mixed $content;
object &$smarty;
boolean &$repeat;
Block functions are functions of the form: {func} .. {/func}. In other words, they enclose a template block and operate on the contents of this block. Block functions take precedence over custom functions of the same name, that is, you cannot have both custom function {func} and block function {func}..{/func}.
By default your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag. (See $repeat below on how to change this.)
Only the opening tag of the block function may have attributes. All attributes passed to template functions from the template are contained in the $params variable as an associative array. The opening tag attributes are also accessible to your function when processing the closing tag.
The value of the $content variable depends on whether your function is called for the opening or closing tag. In case of the opening tag, it will be NULL, and in case of the closing tag it will be the contents of the template block. Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source.
The parameter $repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is TRUE at the first call of the block-function (the opening tag) and FALSE on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with $repeat being TRUE, the contents between {func}...{/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content.
If you have nested block functions, it's possible to find out what the parent block function is by accessing $smarty->_tag_stack variable. Just do a var_dump() [http://php.net/var_dump] on it and the structure should be apparent.
块函数的形式是这样的:{func} .. {/func}。换句话说,他们被封闭在一个模板区域内,然后对该区域的内容进行操作。块函数优先于同名的自定义函数,即是说,你不能同时使用自定义函数{func}和块函数{func} .. {/func}。
默认地,你的函数实现会被Smarty调用两次:一次是在开始标签,另一次是在闭合标签(参考下面的&$repeat关于怎样改变这种设置)。
只有块函数的开始标签具有属性。所有属性包含在作为关联数组的$params变量中,经由模板传递给模板函数。当处理闭合标签时,函数同样可访问开始标签的属性。
$content变量值取决于你的函数是被开始标签调用还是被闭合标签调用。假如是开始标签,变量值将为NULL,如果是闭合标签,$content变量值为模板块的内容。请注意这时模板块已经被Smarty处理过,因此你所接收到的是模板的输出而不是模板资源。
&$repeat参数通过引用传递给函数执行,并为其提供控制块显示多少次的可能性。默认情况下,在首次调用块函数(块开始标签)时&$repeat变量为true,在随后的所有块函数(闭合标签)调用中其值始终为false。函数每次执行返回的&$repeat值为true时,{func} .. {/func}之间的内容会被求值,同时参数$content里的新块内容会再次调用执行函数(译注:运行方法有点类似递归函数)。
如果你嵌套了块函数,可以通过$smarty->_tag_stack变量访问找出父块函数。只须对块函数运行一下[var_dump()](http://php.net/var_dump),函数结构就可以一目了然了。
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN4165" id="AEN4165"> </a> <b>Example 16.5. block function</b> <br/><strong>例16-5.块函数 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><?php/* * Smarty plugin * ------------------------------------------------------------- * File: block.translate.php * Type: block * Name: translate * Purpose: translate a block of text * ------------------------------------------------------------- */function smarty_block_translate($params, $content, $smarty, &$repeat){ // only output on the closing tag if(!$repeat){ if (isset($content)) { $lang = $params['lang']; // do some intelligent translation thing here with $content return $translation; } }}?></pre></td></tr></table><p> 参见<a href="api.registerplugin.html">registerPlugin()</a>、<a href="api.unregisterplugin.html">unregisterPlugin()</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="plugins.modifiers.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="plugins.compiler.functions.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Modifiers<br/> 调节器</td><td width="34%" align="center" valign="top"><a href="plugins.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Compiler Functions<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使用指南
- 翻译人员列表