Home  >  Article  >  Backend Development  >  smarty config_load 和 自定义函数 报错解决方法

smarty config_load 和 自定义函数 报错解决方法

WBOY
WBOYOriginal
2016-06-13 13:31:071047browse

smarty config_load 和 自定义函数 报错
temp.tpl

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->{config_load file="../configs/a.conf"}
{#s#}

a.conf
HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->s='测试测试'

报错
Fatal error: Uncaught exception 'SmartyException' with message 'Unable to read config file '../configs/a.conf'' in C:\wamp\www\smarty\libs\sysplugins\smarty_internal_resource_file.php:68 Stack trace: #0 C:\wamp\www\smarty\libs\sysplugins\smarty_config_source.php(86): Smarty_Internal_Resource_File->getContent(Object(Smarty_Config_Source)) #1 C:\wamp\www\smarty\libs\sysplugins\smarty_internal_config_file_compiler.php(81): Smarty_Config_Source->__get('content') #2 C:\wamp\www\smarty\libs\sysplugins\smarty_internal_config.php(189): Smarty_Internal_Config_File_Compiler->compileSource(Object(Smarty_Internal_Config)) #3 C:\wamp\www\smarty\libs\sysplugins\smarty_internal_config.php(214): Smarty_Internal_Config->compileConfigSource() #4 C:\wamp\www\smarty\templates_c\187938f6ec55068ed23f0f2de8d126b1ae3149c7.file.temp.tpl.php(36): Smarty_Internal_Config->loadConfigVars(NULL, 'local') #5 C:\wamp\www\smarty\libs\sysplugins\smarty_internal_templatebase.php(180): content_4fb7f6591b8ab2_26457252(Object(Smarty_Internal_Template)) #6 C:\w in C:\wamp\www\smarty\libs\sysplugins\smarty_internal_resource_file.php on line 68


自定义函数
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php require_once './libs/Smarty.class.php';
    $smarty=new Smarty;
    
    function myfun($args){
        $str="";
        for($i=0;$i<$args['times'];$i++){
            $str.="测试";
        }
        return $str;
    }
    $smarty->register_function("doloop","myfun");
    $smarty->display("temp.tpl");
?>


temp.tpl
HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->{doloop times="5"}


报错
Fatal error: Uncaught exception 'SmartyException' with message 'Call of unknown method 'register_function'.' in C:\wamp\www\smarty\libs\sysplugins\smarty_internal_templatebase.php:806 Stack trace: #0 [internal function]: Smarty_Internal_TemplateBase->__call('register_functi...', Array) #1 C:\wamp\www\smarty\smarty.php(62): Smarty->register_function('doloop', 'myfun') #2 {main} thrown in C:\wamp\www\smarty\libs\sysplugins\smarty_internal_templatebase.php on line 806

我从smarty官方网站上下载了Smarty 3.1.8 [Smarty-stable.zip] Feb 20th, 2012文件啊
stable这个单词是稳定的意思 我就下了这个包 解压后 我把里面的libs文件夹拷贝到我的www/smarty目录下,
其它像在temp.tpl模板里运行{$smarty.server.SERVER_ADDR}这个语句可以正常显示啊,为什么就运行上面的config_load和自定义函数就不行啊


------解决方案--------------------
google了一下,原来是这样的啊。
3.0以前是用register_function来实现,现在的是新方法registerPlugin。
$smarty->registerPlugin(“function”,”demo”, “demotest”);;//制定类型,这里制定是函数调用,指定模板函数名称和php函数名称
function demotest($params) {
extract($params);//获取参数a与b了
echo $a.$b;//输出
}

模板写法:
{demo a=”2″ b=”test”}
输出:2test

可是模板中使用{config_load}还是要出错啊,怎么解决?
------解决方案--------------------
啊,config_load出错也解决了
我在temp.tpl模板中直接写出{config_load file="a.conf"}
在temp.tpl文件中新建了configs文件夹,并把a.conf放在文件夹里面,
奇怪是的,我没指定a.conf在configs文件夹里啊,它是怎么找到的?
总因该在temp.tpl写成{config_load file="../configs/a.conf"}这对啊,可这样要报错
怎么回事?
smarty3.0是怎么找到它的?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn