MixerGipper
MixerGipper

Reputation: 17

How to render twig template manually without escaping?

I want to render twig template manually, the code is like:

$template = $this->twig->loadTemplate('MyBundle::board.html.twig');                                          $result = $template->render(array('entities'=>$query->getResult()));  
return $result;

When I echo $result, everything works well, Chrome renders all HTML code correctly, but I need to return $result from a function, then render in twig as {{result}}, but I can not use {{result|raw}}.

Any other way to make twig don't escape html?

Upvotes: 0

Views: 1513

Answers (2)

Sergey Bleih
Sergey Bleih

Reputation: 225

Not sure why can't you use {{ result|raw }}. But escaping can be disabled for the whole environment. Here is something I have used for rendering some html from string, with escaping turned off.

$twig = new \Twig_Environment( new \Twig_Loader_String(), 
    array(
        'autoescape' => false
    )
);
$body = $env->render(
    $templateString,
    $parameters
);

Though probably this is not what you are looking for.

Upvotes: 1

goto
goto

Reputation: 8164

I think the escaping should be inside MyBundle::board.html.twig

Upvotes: -1

Related Questions