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

块函数

void smarty_block_name( $params,  
  $content,  
  $template,  
  &$repeat);  
array $params;
mixed $content;
object $template;
boolean &$repeat;
 

块函数的形式是:{func} .. {/func}。 换句话说,它们用标签圈起一个块,然后对这个块的内容进行操作。 块函数优先于同名的自定义函数, 这样,你不能同时有自定义函数{func}和块函数{func}..{/func}

  • 默认你的函数将被Smarty调用两次,在开始标签位置调用和在结束标签位置调用。 (参考下面如何通过$repeat来改变这种情况)

  • 从Smarty 3.1开始,开始标签的返回值将会被显示。

  • 块函数只有在开始标签时才有 属性。 模板中传递给块函数的属性都包含在$params参数数组中。 在处理结束标签时,这些属性也是可用的。

  • $content的值,取决于函数在执行开始标签还是结束标签。 当在开始标签时,它会是null; 当在结束标签时,它会是模板块里面全部的内容。 注意模板块已经是被Smarty处理过的,所以你得到的内容是模板块的输出内容,而不是模板块的源代码。

  • $repeat是一个引用值,能控制块可以被显示多少次。 当块函数被第一次调用时(开始标签),$repeat默认是true; 随后的调用(结束标签)都是false。 每次当块函数返回时将$repeat设成true, {func}...{/func}间的内容会被再次计算, 计算结果保存$content参数内, 在并且函数将被再次执行。

如果使用了嵌套的块函数, 可以通过$smarty->_tag_stack变量来找到父块的函数。 只要对其用一下var_dump() 就可以知道整个层次结构了。

Example 18.5. 块函数


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     block.translate.php
 * Type:     block
 * Name:     translate
 * Purpose:  翻译一块文字
 * -------------------------------------------------------------
 */
function smarty_block_translate($params, $content, Smarty_Internal_Template $template, &$repeat)
{
    // 只在结束标签时输出
    if(!$repeat){
        if (isset($content)) {
            $lang = $params['lang'];
			// 翻译$content的内容
            return $translation;
        }
    }
}
?>

     

参见: registerPlugin(), unregisterPlugin().

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors