@@ -181,7 +181,10 @@ can be anything. You'll use this key to retrieve the message.
181
181
182
182
In the template of the next page (or even better, in your base layout template),
183
183
read any flash messages from the session using the ``flashes() `` method provided
184
- by the :ref: `Twig global app variable <twig-app-variable >`:
184
+ by the :ref: `Twig global app variable <twig-app-variable >`.
185
+ Alternatively, you can use the
186
+ :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface::peek `
187
+ method instead to retrieve the message while keeping it in the bag.
185
188
186
189
.. configuration-block ::
187
190
@@ -196,6 +199,13 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
196
199
</div>
197
200
{% endfor %}
198
201
202
+ {# same but without clearing them from the flash bag #}
203
+ {% for message in app.session.flashbag.peek('notice') %}
204
+ <div class="flash-notice">
205
+ {{ message }}
206
+ </div>
207
+ {% endfor %}
208
+
199
209
{# read and display several types of flash messages #}
200
210
{% for label, messages in app.flashes(['success', 'warning']) %}
201
211
{% for message in messages %}
@@ -214,13 +224,27 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
214
224
{% endfor %}
215
225
{% endfor %}
216
226
227
+ {# or without clearing the flash bag #}
228
+ {% for label, messages in app.session.flashbag.peekAll() %}
229
+ {% for message in messages %}
230
+ <div class="flash-{{ label }}">
231
+ {{ message }}
232
+ </div>
233
+ {% endfor %}
234
+ {% endfor %}
235
+
217
236
.. code-block :: php-standalone
218
237
219
238
// display warnings
220
239
foreach ($session->getFlashBag()->get('warning', []) as $message) {
221
240
echo '<div class="flash-warning">'.$message.'</div>';
222
241
}
223
242
243
+ // display warnings without clearing them from the flash bag
244
+ foreach ($session->getFlashBag()->peek('warning', []) as $message) {
245
+ echo '<div class="flash-warning">'.$message.'</div>';
246
+ }
247
+
224
248
// display errors
225
249
foreach ($session->getFlashBag()->get('error', []) as $message) {
226
250
echo '<div class="flash-error">'.$message.'</div>';
@@ -233,15 +257,17 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
233
257
}
234
258
}
235
259
260
+ // display all flashes at once without clearing the flash bag
261
+ foreach ($session->getFlashBag()->peekAll() as $type => $messages) {
262
+ foreach ($messages as $message) {
263
+ echo '<div class="flash-'.$type.'">'.$message.'</div>';
264
+ }
265
+ }
266
+
236
267
It's common to use ``notice ``, ``warning `` and ``error `` as the keys of the
237
268
different types of flash messages, but you can use any key that fits your
238
269
needs.
239
270
240
- .. tip ::
241
-
242
- You can use the
243
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface::peek `
244
- method instead to retrieve the message while keeping it in the bag.
245
271
246
272
Configuration
247
273
-------------
0 commit comments