3

I have this controller where \Exception is raised (I haven't figured out which SF2 Exception to use yet) upon certain condition. Here is it:

<?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 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 HTML is not rendered when seeing the error page in prod environement.

I tried {{ exception.message|raw }} and also setting autoescape to false as per this answer but it seems to have no effect.

How can I do to make HTML works when displaying the \Exception message in Twig?

Community
  • 1
  • 1
D4V1D
  • 5,805
  • 3
  • 30
  • 65

1 Answers1

0

Where ever in the code you catch the exception is where it is needed to be added to the array you pass to twig. for example

$vars = [];
try {

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


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

//pass $vars to twig
exussum
  • 18,275
  • 8
  • 32
  • 65
  • Thanks @exussum for answering my question but I didn't get everything of it. There is no place where I catch the `\Exception`. The `\Exception` is thrown when the condition is met and Symfony displays the `error.html.twig` view. It's in this view I cannot render HTML. – D4V1D Mar 26 '15 at 13:06
  • It seems it does that automatically yeah. – D4V1D Mar 26 '15 at 13:08