Compiler Functions编译函数

Compiler functions are called only during compilation of the template. They are useful for injecting PHP code or time-sensitive static content into the template. If there is both a compiler function and a custom function registered under the same name, the compiler function has precedence.
编译函数仅在模板编译过程中被调用。对于将PHP代码或对时间敏感的静态内容嵌入到模板中,他们是比较有用的。如果编译函数和普通函数都注册了同一个名字,则编译函数具有优先使用权。

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

The compiler function is passed two parameters: the tag argument string - basically, everything from the function name until the ending delimiter, and the Smarty object. It's supposed to return the PHP code to be injected into the compiled template.
此编译函数有两个参数:标记参数字符串——基本上是从函数名字直到结束位置的所有内容,另一个参数是Smarty对象。该函数将返回嵌入到被编译模板中的PHP代码。

See also register_compiler_function(), unregister_compiler_function().

Example 16-6. simple compiler function简单编译函数

<?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'). "';";
}
?>

This function can be called from the template as:
这个函数在模板中以如下方式调用:

{* this function gets executed at compile time only *}
{tplheader}

The resulting PHP code in the compiled template would be something like this:
在编译之后的模板中被嵌入的PHP代码内容如下:

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