簡體   English   中英

無法添加新的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"}

這是錯誤的:

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因此當我在smarty文件夾中有myplugins文件夾時,如您的情況,我需要使用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 ,該字符帶有我的函數中定義的大寫字母。

因此,如您所見,它可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM