Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #21819 [Twig Bridge] A simpler way to retrieve flash messages…
… (javiereguiluz) This PR was squashed before being merged into the 3.3-dev branch (closes #21819). Discussion ---------- [Twig Bridge] A simpler way to retrieve flash messages | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Getting flash messages in templates is more complex than it could be. Main problems: 1. It's too low level: you need to get the "flash bag" (and first, learn what a "flash bag" is) and then you need to call the internal method: `all()`, `get()`, etc. 2. You need to be careful because the session will start automatically when you ask for flashes (even if there are no flashes). You can prevent this with the `{% if app.session is not null and app.session.started %}` code, but it's boring to always use that. So, I propose to add a new `app.flashes` helper that works as follows. --- ## Get all the flash messages ### Before ```twig {% if app.session is not null and app.session.started %} {% for label, messages in app.session.flashbag.all %} {% for message in messages %} <div class="alert alert-{{ label }}"> {{ message }} </div> {% endfor %} {% endfor %} {% endif %} ``` ### After ```twig {% for label, messages in app.flashes %} {% for message in messages %} <div class="alert alert-{{ label }}"> {{ message }} </div> {% endfor %} {% endfor %} ``` --- ## Get only the flashes of type `notice` ```twig {% if app.session is not null and app.session.started %} {% for message in app.session.flashbag.get('notice') %} <div class="alert alert-notice"> {{ message }} </div> {% endfor %} {% endif %} ``` ### After ```twig {% for message in app.flashes('notice') %} <div class="alert alert-notice"> {{ message }} </div> {% endfor %} ``` --- As an added bonus, you can get any number of flash messages because the method allows to pass an array of flash types: ```twig {% for label, messages in app.flashes(['warning', 'error']) %} {% for message in messages %} <div class="alert alert-{{ label }}"> {{ message }} </div> {% endfor %} {% endfor %} ``` Commits ------- 5a56b23 [Twig Bridge] A simpler way to retrieve flash messages
- Loading branch information