Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(...)?

like image 404
Andrei Herford Avatar asked Apr 08 '16 09:04

Andrei Herford


2 Answers

Not sure i really understand your question. If this is not what you're asking just say it and I will remove this answer.
As said in documentation you use

app.session.flashbag.get('success')

to retrieve your flash message. Example :

{% for flash_message in app.session.flashbag.get('success') %}
        {{ flash_message|raw }}
{% endfor %}
like image 59
Isky Avatar answered Oct 03 '22 17:10

Isky


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 %}
like image 25
Himel Nag Rana Avatar answered Oct 03 '22 16:10

Himel Nag Rana