What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
编译函数仅会在模板的编译过程中调用, 它对模板嵌入PHP代码或者对时间敏感的内容时比较有用。 如果同时存在编译函数和 自定义函数,那么编译函数会优先使用。
mixed smarty_compiler_name( |
$params, | |
$smarty) ; |
array $params
;object $smarty
;编译函数有两个参数:已预编译的变量字符串数组,及Smarty对象。 该函数将会返回嵌入到模板的PHP代码。
Example 18.6. 一个简单的编译函数
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: compiler.tplheader.php * Type: compiler * Name: tplheader * Purpose: 输出模板文件名和编译时间 * ------------------------------------------------------------- */ function smarty_compiler_tplheader($params, Smarty $smarty) { return "<?php\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';\n?>"; } ?>
模板内调用:
{* 此函数仅在编译时执行 *} {tplheader}
在编译后的文件中,PHP代码如下:
<?php echo 'index.tpl compiled at 2002-02-20 20:02'; ?>