<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">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: block.translate.php * Type: block * Name: translate * Purpose: translate a block of text * ------------------------------------------------------------- */function smarty_block_translate($params, $content, $smarty, &amp;$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; } }}?&gt;</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>