Andrei Herford Andrei Herford - 1 month ago 29
PHP Question

How to use HTML tags in Symfony flash message

working with Symfony 2.7 I would like to include HTML in a flash message:

class MyController extends Controller {
public function someAction(Request $request) {
...
$this->addFlash('success', $tranlator->trans('some.success.msg', array(), 'app'));
...
}
}


// app.yml
some:
success:
msg: Text with some <strong>HTML</strong>


This creates a Flash Message

Text with some <strong>HTML</strong>


instead of


Text with some HTML


Within my own Twig template I would use the
raw
filter, to display the HTML code directly instead of the escaped version. But how can I achive the same within
addFlash(...)
?

Answer

I think this cannot be done directly. But you can tweak where you are showing the message using some html and css.

Like:

In setting the flash message:

$this->get('session')->getFlashBag()->add('notice', array('type' => 'success', 'title' => 'Done!', 'message' => 'OK! It is done!'));

And in twig file:

{% for msg in app.session.flashbag.get('notice') %}
<div class="alert alert-{{ msg.type }}">
    <strong>{{ msg.title }}</strong><br/>{{ msg.message }}
</div>
{% endfor %}
Comments