How do you customize the HTML template for flash


Nov '14

Aug '16

1

987

0


Thien
31.3k

You must to create a custom class inheriting from Phalcon\Flash\Direct to alter the message adding the close div:

    class MyFlash extends Phalcon\Flash\Direct
    {
        public function message($type, $message)
        {
            $message .= ' <div class="close"></div>';
            parent::message($type, $message);
        }
    }

Then, you must to setup it in file autoload

    $di->set('flash', function(){
        return new MyFlash();
    })

Or, you can adding button code to your message:

$this->flash->success('<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>My Message');

This is great trick and works great for me until upgrade to phalcon 2.1.x, the trick won't work well because of autoescape, https://github.com/phalcon/cphalcon/issues/11448.

To get it solved, you have to disable autoescape, so the extra html is not coverted to html entity.

    $di->set('flash', function(){
        $flash = new MyFlash();
        $flash->setAutoescape(false);
        return $flash;
    })