Php Smarty模板引擎-文件获取内容

Php Smarty模板引擎-文件获取内容,php,json,smarty,Php,Json,Smarty,我尝试使用smarty读取包含文件内容的外部数据 然而,我得到了这个错误 Fatal error: Smarty error: [in /opt/lampp/htdocs/blog/serendipity/templates/templates3/index.tpl line 107]: [plugin] (secure mode) modifier 'file_get_contents' is not allowed (Smarty_Compiler.class.php, line 1934)

我尝试使用smarty读取包含文件内容的外部数据

然而,我得到了这个错误

Fatal error: Smarty error: [in /opt/lampp/htdocs/blog/serendipity/templates/templates3/index.tpl line 107]: [plugin] (secure mode) modifier 'file_get_contents' is not allowed (Smarty_Compiler.class.php, line 1934) in /opt/lampp/htdocs/blog/serendipity/bundled-libs/Smarty/libs/Smarty.class.php on line 1093

是否有其他方法获取数据?或者如何允许smarty使用此功能?

您应该做的是配置smarty安全设置

源代码如下:

if ($smarty->security && !in_array($_name, $smarty->security_settings['MODIFIER_FUNCS'])) {
    $_message = "(secure mode) modifier '$_name' is not allowed";
} else {
    if (!function_exists($_name)) {
        $_message = "modifier '$_name' is not implemented";
    } else {
        $_plugin_func = $_name;
        $_found = true;
    }
}

错误表明您处于安全模式。这意味着Smarty不允许您运行PHP脚本(取决于安全模式级别)或调用许多PHP函数

您可以关闭我不推荐的安全模式,也可以将PHP代码放入控制器并在PHP控制器中分配var:

...
$data = file_get_contents('path_to_json');
$smarty->assign('data', $data);
...


也许这个插件在这里会有所帮助。无论如何,@shadyyx没有错。您可能只想分配内容,让您的生活更简单。

您是否将此称为smarty?您应该使用$smarty->assign('var',$var),从PHP文件将其分配给smarty;编辑你能展示一些你想做什么的代码,或者你在哪里使用它吗?你到底想做什么,为什么要在tpl文件中使用?{assign var=data value='}{$data |@print_r}我确实这么做了,我对JSON不太了解,但试着通过你的控制器(PHP)分配那个值。类似这样的内容:$smarty->assign('data','path.to/JSON'| file_get_contents);然后在.tpl中说{$data}。
$smarty->assign('data', file_get_contents('path_to_json'));