Smarty主题引擎由Travis Cline维护,它可以在Drupal主题中方便的使用Smarty模板引擎语法.它是从PHPTemplate主题引擎移植过来的.
正是由于它是移植过来的,所以大多数情况下, PHPTemplate的文档都可用来作为参考.
与PHPTemplate类似,它使用单独的something.tpl(注意扩展名是.tpl 而不是.tpl.php)文件,来覆写Drupal的主题函数theme_something().Drupal的可主题化函数的相关文档可参看Drupal API站点: http://api.drupal.org.
每个模板文件都包含一个HTML骨架,里面嵌套了用来输出动态内容的Smarty标签。Smarty标签比php标签更具有可读性。
下面的主题是从PHPTemplate格式转化为Smarty主题引擎格式的:
为了能够使用基于Smarty的Drupal主题,比如Box_grey_smarty, Bluemarine_smarty, Leaf_smarty,等等,你需要首先安装Smarty主题引擎.
现在,你就可以启用,配置,和使用基于Smarty的Drupal主题了。
相关链接:http://drupal.org/node/27566 , Think in Drupal
Web服务器进程应该对themes/engines/smarty目录下面的templates_c子目录,具有读和写的权限.
在linux/unix环境下,解决方案如下:
如果你有chown访问权限的话:
该目录应该由你的用户拥有,一个小组也拥有这个目录的所有权,这个小组中就包括你的web服务器。比如(你的用户名称):apache。
所有者及小组成员对这个目录具有“读/写/执行”的权限,而其它用户具有“读/执行”的权限。
你可以使用<?php phpinfo(); ?>来查看'apache'是不是属于这个用户组的。
chmod 775 templates_c
chown (your user name):apache templates_c
更可能的是,你没有chown访问权:
那么,一个简单的命令:
chmod 777 templates_c
就能为这个目录设置正确的权限了。
如果具有chown访问权的话,就是用前者。最好不要让所有人对这个目录都具有写的权限,但是很多情况下,不得不如此。
在win32下面,你也必须让web服务器进程对templates_c子目录具有写的权限。
在你的Drupal主题目录下,创建一个名为smartytemplate.php的文件.
<?php
function _smarty_variables($hook, $vars = array()) {
switch ($hook) {
case 'page':
if ((arg(0) == 'admin')) {
$vars['template_file'] = 'admin';
}
break;
}
return $vars;
}
?>
参看:
由于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.
如果需要Drupal Smarty主题系统的高级特性的话,那么你需要在你的主题目录下面创建一个额外的文件(例如, e.g. themes/blumarine_smarty/smartytemplate.php).
这个文件可用来
下面是一个覆写主题函数的例子:
(是从phptemplate中借鉴过来的)
首先你需要找到要覆写的主题函数的所在位置。你可以在Drupal API文档中找到一列这样的例子。在这里我们以theme_item_list()为例。
如果你要覆写的主题函数不包含在基本列表中(block, box, comment, node, page),你需要将它告诉给Smarty主题引擎.
为了实现这一点,你需要在你主题目录下,创建一个t smartytemplate.php文件.这个文件应该包含必须的<?php …?>标签,还有用于主题覆写的存根(stubs),这些存根告诉引擎使用哪个模板文件,以及向其中传递哪些参数.
theme_item_list()大概是这样定义的:
<?php
function theme_item_list($items = array(), $title = NULL, $type = 'ul') {
?>
现在,你需要在你主题的smartytemplate.php文件中加一个存根,如下所示:
<?php
/**
* Catch the theme_item_list function, and redirect through the template api
*/
function smarty_item_list($items = array(), $title = NULL, $type = 'ul') {
// Pass to Smarty Theme Engine, translating the parameters to an associative array. The element names are the names that the variables
// will be assigned within your template.
return _smarty_callback('item_list', array('items' => $items, 'title' => $title, 'type' => $type));
}
?>
我们将函数名中的“theme”替换为了“smarty”,并调用函数_ smarty _callback()以将参数($items和$title)传递给Smarty主题引擎。
另外,在函数名中,除了'smarty'以外,你还可以使用主题的名字(例如,如果主题为bluemarine_smarty的话,那么bluemarine_smarty_item_list 和smarty_item_list都可以使用)。
现在,你可以在你主题目录下,创建一个item_list.tpl模板文件了,它将用来替代原有的theme_item_list()。这个函数,与原有的theme_item_list()相比,应该采用相同的逻辑。
有时,将显示委托给模板文件并不合适。如果直接使用php会更好的话,你在这里可以不用调用_smarty_callback(),而直接返回输出结果。