Render twig content from a string
0
If you want to render twig content contained in a string (a textarea field for example), you can do so with a twig filter as shown below:
Add this twig filter to your Plugin.php:
use Cms\Classes\Controller;
public function registerMarkupTags()
{
return [
'filters' => [
'twig' => function ($content, $vars=[]) {
$controller = Controller::getController() ?: new Controller;
$vars = array_merge($vars, $controller->vars);
$env = $controller->getTwig();
return $env->createTemplate($content)->render($vars);
},
],
];
}
You can now render any twig content in your markup as shown below:
{{ fieldWithTwigContent | twig }}
There are no comments yet
Be the first one to comment