Show all flash bag contents in Symfony Twig view

Here is a how you can display all your flash bag messages in a Symfony Twig view.  The “flash bag” is a special collection of session variables that hold data like a one-time message in HTML.  It is often used in e-commerce form flows when the credit card was declined, instead of further processing the payment, the rejection of the credit card appears in your TWIG view so the user can read the message and act on it.

Perhaps it will say “Credit card was denied, please check your billing ZIP code.”  or another frequent issue happens when the user fails to put in the appropriate number of credit card digits, you can have your controller put in the flash bag the message:  “Credit card requires 15 digits for AMEX, 16 digits for all other cards.”

{% for label, flashes in app.session.flashbag.all %}

    {% for flash in flashes %}

        <div class="alert alert-{{ label }}">

            {{ flash }}

        </div>

    {% endfor %}

{% endfor %}

 

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *