不能添加新的Smarty插件


Can't add new Smarty plugins

目录结构:
smarty
------plugins
------myplugins
---------------function.myfnc.php

文件function.myfnc.php:

function smarty_function_myfnc($params, &$smarty) {
   ///code here
}

添加插件:

$smary->addPluginsDir(/path/to/myplugins);

但是,当我调用文件display.tpl:

{myfnc p="value"}

this is error:

Call to undefined function smarty_function_myfnc()
有人能帮帮我吗?

问题可能出在插件目录的路径上。您应该在myplugins之前添加与需要Smarty.class.php时相同的路径。

例如我的代码:

<?php
require 'smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->setTemplateDir('templates');
$smarty->addPluginsDir('smarty/myplugins');
var_dump($smarty->getPluginsDir());

echo $smarty->fetch('temp.tpl');

一开始我有require 'smarty/Smarty.class.php'; -我到Smarty.class.php的路径是smarty,所以当我有myplugins文件夹时,它在smarty文件夹内,就像你的情况一样,我需要使用smarty/myplugins而不仅仅是myplugins

然后我有templates/temp.tpl文件内容:
{myfnc p="value"}

和文件smarty/myplugins/function.myfnc.php的内容:

<?php
function smarty_function_myfnc($params, Smarty_Internal_Template $template)
{                           
    return strtoupper($params['p']);
}
?>

模板的输出是在我的函数中定义的大写字母的VALUE

正如你所看到的,它可以正常工作