Search code examples
yii2twigsmartyrender

Yii2 render database content


There is a textarea where user can edit some templates and can use variables like:

{{model.user.name}}

Application need to replace this variables with data and display HTML output.

We can write a small function that will replace variables from template with data but we need to use a template engine like Twig or Smarty.

https://github.com/yiisoft/yii2-twig https://github.com/yiisoft/yii2-smarty

Now we can use ViewRenderer from Smarty or Twig.

$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
       'model' => $model,
]);

But I see this error:

Unable to find template "template.twig" (looked into: .).

How can I use Smarty or Twig to render a template with the content from database in Yii2 ?


Solution

  • I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:

    $m = new \Mustache_Engine();
    echo $m->render("Hello {{model.client.firma}}",['model' => $model]);