Smarty模板引擎-在使用$ Smarty ->fetch时卸载/禁用插件


Smarty Template Engine - Unload/Disallow plugins while using $smarty->fetch

使用$smarty->fetch('mytemplate.tpl')方法时如何从模板中卸载或禁用smarty core插件

例如模板mytemplate.tpl包含{html_options}{html_table}

当使用$smarty->fetch('mytemplate.tpl')时,smarty只解析{html_options},而不解析{html_table}

从插件文件夹中删除function.html_table.php不是一个选项,因为它仍然在使用另一个$smarty->fetch()调用

一种可能的解决方案是从Smarty_Security类扩展,并通过使用

方法启用安全性

$smarty->enableSecurity($instanceOfClass)

一旦fetch方法被调用,disableSecurity方法就会重新启用所有的plugins/tags/modifiers

不幸的是,当使用enableSecurity和禁用函数时,会抛出异常

另一种方法是替换所有的标签/变量/…您希望在调用 $smarty->fetch([...])

之前使用preg_replace 来禁用{literal}{forbiddenTags}{/literal}

例子
# negate regular expression pattern to allow only the below tags
$pattern = "/'{(?!allowedTag1|allowedTag2).*?'}/";
$replacement = '{literal}$0{/literal}';
$content = preg_replace($pattern, $replacement, $content);
$smarty->fetch("string:" . $content);

关于Security类的更多详细信息请参见:http://www.smarty.net/docs/en/advanced.features.tpl