You are here

为drupal Smarty模板中添加额外的变量

g089h515r806 的头像
Submitted by g089h515r806 on 星期五, 2008-09-19 07:59

由于Smarty主题引擎是从phptemplate移植过来的,所以可参考phptemplate的解决方案.

 

在你主题目录下的smartytemplate.php文件(参看: smartytemplate.php:你主题的发动机)中,通过实现一个_smarty_variables函数,就可以简单的添加额外的变量了.

 

例如

假如你的drupal默认主题为box_grey_smarty:

在themes/box_grey_smarty/目录下,添加一个smartytemplate.php文件,将下面的内容拷贝进去:

 

<?php
function  _smarty_variables($hook, $variables) {
  switch($hook) {
    case 'comment' :
        $variables['newvar'] = 'new variable';
        $variables['title'] = 'new title';
        break;
  }
  return $variables;
}
?>

 

接着,你就可以在你的comment.tpl使用新的变量{$newvar}了.

 

更多例子可参看phptemplate的模板变量添加,记住将_phptemplate_variables替换为_smarty_variables.

 

 相关链接: http://drupal.org/node/70249 , Think in Drupal

Drupal版本: