smarty config_load 和 自定义函数 报错

q59185918 2012-05-20 04:30:07
temp.tpl
{config_load file="../configs/a.conf"}
{#s#}

a.conf
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
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
{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和自定义函数就不行啊
...全文
217 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
googleelgoog 2012-05-20
  • 打赏
  • 举报
回复
啊,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是怎么找到它的?
googleelgoog 2012-05-20
  • 打赏
  • 举报
回复
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}还是要出错啊,怎么解决?

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧