{{exception.message}} in Twig does not render HTML

I have this controller where it is \Exception

raised (I haven't figured out which SF2 to Exception

use yet ) under certain conditions. Here he is:

<?php

namespace My\AppBundle\Controller;

use ....

class MyController extends Controller
{
     const EXCEPTION_MESSAGE = <<<EOF
My <b>HTML</b>
<br/>
<small>Small phrase</small>
EOF;

     public function indexAction()
     {
         // my logic

         if(in_array($data, $array))
             throw new \Exception(self::EXCEPTION_MESSAGE);

         // the rest of my logic

         return ....
     }

}

      

And in the app /Resources/TwigBundle/views/Exception/error.html.twig

{% extends '::base.html.twig' %}

{% block body %}
    <h2>Error</h2>
    <p>{{ exception.message }}</p>
{% endblock %}

      

The problem is that the HTML is not displayed when you view the page with an error in the prod

environement.

I tried {{ exception.message|raw }}

and also set autoescape

to false

according to this answer but doesn't seem to have any effect.

How can I make HTML work when displaying a post \Exception

in Twig?

+1


source to share


1 answer


Wherever in the code, you will catch an exception where it needs to be added to the array you are passing to the branch. eg



$vars = [];
try {

    $a->indexAction();
    //fill $vars 


} catch (Exception $e) {
  $vars['exception'] = $e;
}

//pass $vars to twig

      

0


source







All Articles