【发布时间】:2016-02-14 07:46:53
【问题描述】:
有一个文本区域,用户可以在其中编辑一些模板并可以使用如下变量:
{{model.user.name}}
应用程序需要用数据替换这个变量并显示 HTML 输出。
我们可以编写一个小函数,将模板中的变量替换为数据,但我们需要使用像 Twig 或 Smarty 这样的模板引擎。
https://github.com/yiisoft/yii2-twig https://github.com/yiisoft/yii2-smarty
现在我们可以使用 Smarty 或 Twig 的 ViewRenderer。
$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
'model' => $model,
]);
但我看到了这个错误:
Unable to find template "template.twig" (looked into: .).
如何使用 Smarty 或 Twig 在 Yii2 中使用来自数据库的内容呈现模板?
【问题讨论】: