Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash-Messages in Symfony2 doesn't seem to work in my twig-template

I want to add support for flash messages on our pages. I implemented this by following the documentation found here.

I added the following snipplet to my base layout. (i also tried to add it to a specific action template).

{% if app.session.hasFlash('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flash('notice') }} 
   </div> 
{% endif %} 

After adding the following error is thrown

Twig_Error_Runtime: Item "hasFlash" for "" does not exist in "MyBundle::layout.html.twig" at line 66

Is there anything else i need to do ?

like image 867
stijink Avatar asked Nov 23 '11 09:11

stijink


3 Answers

do you use symfony 2.0 or 2.1 (currently master branch)?

for symfony 2.1 the documentation is located here: http://symfony.com/doc/2.1/book/controller.html#flash-messages

flash messages are shown like this:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{% endfor %}
like image 53
c33s Avatar answered Nov 13 '22 10:11

c33s


Mmm check in your config file that you have auto-started the session:

session:
    default_locale: %locale%
    auto_start:     true

Because the error seems to be that Twig doesn't find the session class, not something about the hasFlash function. In fact I have almost exactly the same code in my layout.

like image 42
Silence Avatar answered Nov 13 '22 08:11

Silence


This is pretty old at time of writing so imagine you've worked it out by now, but for reference sake, it's has rather than hasFlash. So..

{% if app.session.flashbag.has('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flashbag.get('notice') }} 
   </div> 
{% endif %} 
like image 3
Jenko Avatar answered Nov 13 '22 10:11

Jenko