我有一个php变量,其中包含html/smarty代码
$x='<a href="{$link}" >{$title}</a>';
Run Code Online (Sandbox Code Playgroud)
这个数据是从数据库中获取的,我想用smarty对它进行评估,并将输出放入php变量(将其打印出来或再次将其保存到数据库中).
谢谢
编辑:
我希望使用smarty评估X的内容,就好像x的内容存储在file.tpl中然后$ y = $ smarty-> fetch('file.tpl'); ...想要这样做而不需要将x的内容保存到文件中
mez*_*eze 13
如果您正在使用Smarty 3,您可以轻松完成
$smarty->fetch('string:'.$template_string);
Run Code Online (Sandbox Code Playgroud)
或'eval:'.$template_string.更多关于它的手册
小智 5
如果您没有使用Smarty 3并且没有字符串/ eval资源,则可以使用Smarty eval插件.我发现这比创建自定义资源简单得多,问题也少得多.
$template = "put some {$variables} in here"
require_once( $smarty->_get_plugin_filepath( 'function', 'eval' ));
$compiled = smarty_function_eval(array('var'=>$template), $smarty);
Run Code Online (Sandbox Code Playgroud)
上面的例子都不适合我,可能是因为我们目前使用的是旧版本的 smarty。一个对我们有用的解决方案是创建一个模板,我们称之为eval.tpl仅包含以下行:
{eval var=$string}
Run Code Online (Sandbox Code Playgroud)
然后,当我们想要评估字符串时,我们可以简单地使用以下内容:
$smarty->assign('string', $string);
$result = $smarty->fetch('eval.tpl');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8387 次 |
| 最近记录: |