Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add template in flash messages symfony2

After the create and delete object, I redirect to the same action(indexAction). After create I would like render this bootstrap messages as flash messages:

<div class="alert alert-success" role="alert">
  <a href="#" class="alert-link">...</a>
</div>

but after delete, I would like render another block of html:

<div class="alert alert-danger" role="alert">
  <a href="#" class="alert-link">...</a>
</div>

What is the best way to pass this to flash messages? Because, I think passing all html is not good idea?

$this->get('session')->getFlashBag()->add(
        'notice',
        '<div class="alert alert-danger" role="alert">
             <a href="#" class="alert-link">...</a>
         </div>'
    );

Is any exist better way to solve this problem?

like image 222
Thomas Shelby Avatar asked Aug 08 '14 22:08

Thomas Shelby


3 Answers

Here is what I usualy do:

{% for type, flashes in app.session.flashbag.all %}
    {% for flash in flashes %}
        <div class="alert alert-{{ type }} fade in">
            {{ flash }}
        </div>
    {% endfor %}
{% endfor %}

From a controller:

$this->addFlash('success', 'What an awesome message !');

Will create an alert-success

like image 160
Pierre de LESPINAY Avatar answered Oct 21 '22 04:10

Pierre de LESPINAY


In your controller

$this->get('session')->getFlashBag()->add('info', 'info message');

In your view

{% for message in app.session.flashbag.get('info') %}
    <div class="alert alert-info alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
        </button>
        <p>{{ message }}</p>
    </div>
{% endfor %}
like image 40
Fidan Hakaj Avatar answered Oct 21 '22 05:10

Fidan Hakaj


I have found better solution.

In my controller:

$this->get('session')->getFlashBag()->add(
    'notice',
    array(
        'alert' => 'success',
        'title' => 'Success!',
        'message' => 'New word has been added successfully.'
    )
);

This is my view:

{% if app.session.started %}
    {% for flashMessage in app.session.flashbag.get('notice') %}
        <div class="alert alert-{{ flashMessage.alert }} alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert">
                <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
            </button>
            <strong>{{ flashMessage.title }}</strong> {{ flashMessage.message }}
        </div>
    {% endfor %}
{% endif %}
like image 23
Thomas Shelby Avatar answered Oct 21 '22 05:10

Thomas Shelby