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

php中文网
发布: 2016-06-13 13:31:07
原创
1165人浏览过

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是怎么找到它的?
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号