Template Functions

PHP Smarty

Smarty - the compiling PHP template engine
Prev 来源:PHP中文社区 Chapter 16. Extending Smarty With Plugins Next

Template Functions模板函数

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File: function.eightball.php
 * Type: function
 * Name: eightball
 * Purpose: outputs a random magic answer
 * -------------------------------------------------------------
 */
function smarty_function_eightball($params, &$smarty)
{
 $answers = array('Yes',
 'No',
 'No way',
 'Outlook not so good',
 'Ask again soon',
 'Maybe in your reality');

 $result = array_rand($answers);
 return $answers[$result];
}
?>

Question: Will we ever have time travel?
Answer: {eightball}.
<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File: function.assign.php
 * Type: function
 * Name: assign
 * Purpose: assign a value to a template variable
 * -------------------------------------------------------------
 */
function smarty_function_assign($params, &$smarty)
{
 extract($params);

 if (empty($var)) {
 $smarty->trigger_error("assign: missing 'var' parameter");
 return;
 }

 if (!in_array('value', array_keys($params))) {
 $smarty->trigger_error("assign: missing 'value' parameter");
 return;
 }

 $smarty->assign($var, $value);
}
?>


Prev 来源:PHP中文社区 上一页 Home 回顶部 Next 下一页
Writing Plugins编写插件 Up 向上 Modifiers修饰插件