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