We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Flashing Message => Insert Close Button worked in 2.0 not in 3.0

Hello,

In my previous projects, I wanted to insert a close button in the Flash Message.

My solution was:

$this->view->flashClose='<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';

If Message: $this->flashSession->success($this->view->flashClose."Message Content");

This worked fine for my previous projects. But I started a new project running with the last phalcon version and it doesn't work anymore. In fact quotes are added including the html button content.

If someone has a solution, that would be great

Many thanks for your help

Try to disable autoescape mode in generated html:

use Phalcon\Flash\Session;

$di->setShared(
    'flashSession',
    function () {
        $flash =  new Session([
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning',
        ]);

        $flash->setAutoescape(false);

        return $flash;
    }
);

In my opinion better way is to define partial with close button:

<div class="aller-wrapper">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    {{ flash.output() }}
</div>

There is one more solution. So you can use alternative escaper such as:

class FlashEscaper extends \Phalcon\Escaper
{
    public function escapeHtml($text)
    {
        $prefix = '<button type="button" class="close" data-dismiss="alert">&times;</button>';

        return $prefix . parent::escapeHtml($text);
    }
}

Then modify service:

$di->setShared(
    'flashSession',
    function () {
        $flash =  new \Phalcon\Flash\Session([
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning',
        ]);

        $flash->setAutoescape(false);
        $falsh->setEscaperService(new \FlashEscaper);

        return $flash;
    }
);

And output where you want:

{{ flash.output() }}