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';
?>
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章