Get Smarty

Donate

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

Buy cheap glasses from Cheapglasses123.com and save up to 80%.

Where to buy prescription glasses? EyewearCanada.com is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Shop for 2018 World Cup Football Boots Online sale at guthjcc.com

Browse best 2018 world cup soccer cleats and shoes from SoccerAzteca.com

Advertisement

编译函数

编译函数仅会在模板的编译过程中调用, 它对模板嵌入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';
?>

     

参见: registerPlugin(), unregisterPlugin().

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 11 plus 2? (Are you human?)

Advertisement