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';
?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值