Automatically closing bootstrap alerts
02/10/2015

Say you have the following in your template, in order to show flashMessages in your twig template:

{% for flashMessage in app.session.flashbag.get('alert-danger') %}
    
{{ flashMessage }}
{% endfor %}

And the following in your controller:

$this->addFlash(
    'alert-danger',
    'And error has occured!'
);

You can now add this simple piece of javascript to your template:

$(".alert").delay(4000).slideUp(200, function() {
    $(this).alert('close');
});

Wich will automatically close any flashMessage rendered, after 4 seconds.



Like this article? Share it!