Skip to content

Commit

Permalink
Simplified the template code used to render flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Aug 31, 2015
1 parent a46c948 commit 7507a50
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 1 addition & 3 deletions app/Resources/views/admin/blog/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
{% block main %}
<h1>{{ 'title.edit_post'|trans({'%id%': post.id}) }}</h1>

<div class="messages">
{{ messages.flashes([ 'post.updated_successfully' ], 'success') }}
</div>
{{ helper.render_flash_messages() }}

{{ include('admin/blog/_form.html.twig', {
form: edit_form,
Expand Down
4 changes: 1 addition & 3 deletions app/Resources/views/admin/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
{% block main %}
<h1>{{ 'title.post_list'|trans }}</h1>

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

<table class="table table-striped">
<thead>
Expand Down
7 changes: 5 additions & 2 deletions app/Resources/views/admin/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#}
{% extends 'base.html.twig' %}

{# Import macros needed for displaying flash messages. See http://twig.sensiolabs.org/doc/tags/import.html #}
{% import 'macro/messages.html.twig' as messages %}
{#
Import macros used for displaying flash messages.
See http://twig.sensiolabs.org/doc/tags/import.html
#}
{% import 'macro/messages.html.twig' as helper %}

{% block header_navigation_links %}
<li>
Expand Down
32 changes: 17 additions & 15 deletions app/Resources/views/macro/messages.html.twig
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{#
Macros are comparable with functions in regular programming languages.
They are useful to put often used HTML idioms into reusable elements to not repeat yourself.
They are useful to put often used HTML idioms into reusable elements
to not repeat yourself.
See http://twig.sensiolabs.org/doc/tags/macro.html
#}
{% macro flashes(flash_keys, alert_class = 'info') %}
{% from _self import alert %}
{% for flash_key in flash_keys %}
{% for flash_message in app.session.flashBag.get(flash_key) %}
{{ alert(flash_message|trans, alert_class) }}
{% macro render_flash_messages() %}
{% from _self import alert %}

<div class="messages">
{% for flash_message in app.session.flashBag.get('success') %}
{{ alert(flash_message|trans) }}
{% endfor %}
{% endfor %}
</div>
{% endmacro %}

{% macro alert(text, class = 'info') %}
{% spaceless %}
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
<div class="alert alert-dismissible alert-{{ class }}" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
{{- text -}}
</div>
{% endspaceless %}
{% macro alert(text, class = 'success') %}
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
<div class="alert alert-dismissible alert-{{ class }}" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
{{ text }}
</div>
{% endmacro %}
Binary file modified app/data/blog.sqlite
Binary file not shown.

0 comments on commit 7507a50

Please sign in to comment.