编译器函数

    编译器函数只有在模板编译时才被调用。可以用来在模板中注入PHP代码或者时间敏感的静态内容。如果一个编译器函数和一个定制函数注册于同一个名字,那么编译器函数优先。

mixed smarty_compiler_name (string $tag_arg, object &$smarty)

    编译器函数要两个参数:标记参数字符串,一般而言,是从函数名到结尾分隔符的所有东西,以及Smarty对象。它应该返回注入到编译模板中的PHP代码。

例子16-6. 简单的编译器函数

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     compiler.tplheader.php
 * Type:     compiler
 * Name:     tplheader
 * Purpose:  Output header containing the source file name and
 *           the time it was compiled.
 * -------------------------------------------------------------
 */
function smarty_compiler_tplheader($tag_arg, &$smarty)
{
    return 
"\necho '" $smarty->_current_file " compiled at " date('Y-m-d H:M'). "';";
}
?>

    在模板中可以这样调用该函数:

{* 该函数只在编译时得到执行 *}
{tplheader}

    编译好的模板中的PHP代码可能会是:

<?php
echo 'index.tpl compiled at 2002-02-20 20:02';
?>

    参见register_compiler_function()unregister_compiler_function()