为什么Smarty不能加载这个block标签插件?


Why can't Smarty load this block tag plugin

目录结构如下:

。/smartytest.php
./smarty31/* (lib等)
。/插件/block.sayhi.php

初始化smarty的PHP代码如下:

require_once('smarty31/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = getcwd() . '/templates';
$smarty->compile_dir  = getcwd() . '/templates_c';
$smarty->plugins_dir[] = getcwd() . '/plugins';

插件的PHP代码是:

<?php
    function smarty_block_sayhi($params, $content, $smarty, $open) {
        if (!$open) {
            return 'Hello: ' . $content;
        }
    }
?>

我得到的错误信息是:

致命错误:未捕获的异常'SmartyCompilerException'带有消息"模板语法错误"/mypath/phptests/templates/page。tpl"第11行"{sayhi}"未知标签"sayhi"'

当插件在smarty31/lib/plugins目录下时,它加载得很好。这个示例代码没有正确初始化Smarty吗?

$smarty->plugins_dir[] = getcwd() . '/plugins';
应:

$existing = $smarty->getPluginsDir();
$existing[] = getcwd() . '/plugins';
$smarty->setPluginsDir($existing);

原来我正在看一个PHP 4的例子;Smarty 3.1使用PHP 5的访问修饰符,所以我不能这样修改plugins_dir。