Skip to content

Commit 7507a50

Browse files
committed
Simplified the template code used to render flash messages
1 parent a46c948 commit 7507a50

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

app/Resources/views/admin/blog/edit.html.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
{% block main %}
66
<h1>{{ 'title.edit_post'|trans({'%id%': post.id}) }}</h1>
77

8-
<div class="messages">
9-
{{ messages.flashes([ 'post.updated_successfully' ], 'success') }}
10-
</div>
8+
{{ helper.render_flash_messages() }}
119

1210
{{ include('admin/blog/_form.html.twig', {
1311
form: edit_form,

app/Resources/views/admin/blog/index.html.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
{% block main %}
66
<h1>{{ 'title.post_list'|trans }}</h1>
77

8-
<div class="messages">
9-
{{ messages.flashes([ 'post.created_successfully', 'post.deleted_successfully' ], 'success') }}
10-
</div>
8+
{{ helper.render_flash_messages() }}
119

1210
<table class="table table-striped">
1311
<thead>

app/Resources/views/admin/layout.html.twig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
#}
88
{% extends 'base.html.twig' %}
99

10-
{# Import macros needed for displaying flash messages. See http://twig.sensiolabs.org/doc/tags/import.html #}
11-
{% import 'macro/messages.html.twig' as messages %}
10+
{#
11+
Import macros used for displaying flash messages.
12+
See http://twig.sensiolabs.org/doc/tags/import.html
13+
#}
14+
{% import 'macro/messages.html.twig' as helper %}
1215

1316
{% block header_navigation_links %}
1417
<li>
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
{#
22
Macros are comparable with functions in regular programming languages.
3-
They are useful to put often used HTML idioms into reusable elements to not repeat yourself.
3+
They are useful to put often used HTML idioms into reusable elements
4+
to not repeat yourself.
45
See http://twig.sensiolabs.org/doc/tags/macro.html
56
#}
6-
{% macro flashes(flash_keys, alert_class = 'info') %}
7-
{% from _self import alert %}
8-
{% for flash_key in flash_keys %}
9-
{% for flash_message in app.session.flashBag.get(flash_key) %}
10-
{{ alert(flash_message|trans, alert_class) }}
7+
{% macro render_flash_messages() %}
8+
{% from _self import alert %}
9+
10+
<div class="messages">
11+
{% for flash_message in app.session.flashBag.get('success') %}
12+
{{ alert(flash_message|trans) }}
1113
{% endfor %}
12-
{% endfor %}
14+
</div>
1315
{% endmacro %}
1416

15-
{% macro alert(text, class = 'info') %}
16-
{% spaceless %}
17-
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
18-
<div class="alert alert-dismissible alert-{{ class }}" role="alert">
19-
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
20-
{{- text -}}
21-
</div>
22-
{% endspaceless %}
17+
{% macro alert(text, class = 'success') %}
18+
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
19+
<div class="alert alert-dismissible alert-{{ class }}" role="alert">
20+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
21+
<span aria-hidden="true">&times;</span>
22+
</button>
23+
{{ text }}
24+
</div>
2325
{% endmacro %}

app/data/blog.sqlite

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)