Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon 2 output flash with twig template

Hello All,

I'm using twig as the template engine, works good, but I'm having an issue outputing the flash messages there. none of these show anything.

{{ flash()|raw }} {{ flash() }} {{ this.flash.output() }}

Only {{ content() }} works, but it outputs notice and php warning messages as well.

This is how I set the flash

$di->set('flash', function () {

$flash = new FlashDirect([
    //tie in with twitter bootstrap classes
    'error'     => 'alert alert-danger',
    'success'   => 'alert alert-success',
    'notice'    => 'alert alert-info',
    'warning'   => 'alert alert-warning'
]);

return $flash;

});

This how I pass the messages to flash $this->flash->error('Please use the link sent to you by email');

Any help would be appreciated.



67.6k

you need those both services

use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;

$di->set(
    "flash",
    function () {
        return new FlashDirect();
    }
);

// Set up the flash session service
$di->set(
    "flashSession",
    function () {
        return new FlashSession();
    }
);
edited Oct '16

Stupid question but why even use twig ? What's a problem with volt which is much much much faster than twig and have same syntax ? If anything is missing you can easly add it to it.

Are you sure your twig engine extends Injectable ?



862

The problem why I'm using twig, is the developer before me used them, I can't retract right now, as it would delay the project. twig is quite buggy with little documentation.

Stupid question but why even use twig ? What's a problem with volt which is much much much faster than twig and have same syntax ? If anything is missing you can easly add it to it.

Are you sure your twig engine extends Injectable ?



862

Already there

use Phalcon\Flash\Direct as FlashDirect; use Phalcon\Flash\Session as FlashSession;

The problem the messages are not stored in the flash but in the content

you need those both services

use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;

$di->set(
   "flash",
   function () {
       return new FlashDirect();
   }
);

// Set up the flash session service
$di->set(
   "flashSession",
   function () {
       return new FlashSession();
   }
);

Show us your twig engine class. Does it extends Injectable ? What you mean quite buggy ? Volt is not buggy if you meant it, as well documentation is great. Same twig.



862
Accepted
answer

I meant twig, anywas I was able to fix it by using {{ content }} instead of {{ flash.output }}. not sure if it's the best solution.

Show us your twig engine class. Does it extends Injectable ? What you mean quite buggy ? Volt is not buggy if you meant it, as well documentation is great. Same twig.

edited Oct '16

No its not. Show us your engine template for twig. It should work by flash.ouput()

You didn't even posted HOW you are using this twig with phalcon. Just twig template engine needs to extends Phalcon\Mvc\View\Engine and correctly bet set as template engine for view. Then it should work by {{ flash.output() }}



862

How I'm setting the view

$di->set('view', function () use ($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines([ '.twig' => function ($view, $di) use ($config) { return new TwigEngine($view, $di, ['cache' => $config->application->cacheDir . 'twig/']); } ]);

return $view;

}, true);

and I added this function to twig:

        new Twig_SimpleFunction('flash', function () use ($di) {
            return $di->get('flash')->output();
        })

No its not. Show us your engine template for twig. It should work by flash.ouput()

You didn't even posted HOW you are using this twig with phalcon. Just twig template engine needs to extends Phalcon\Mvc\View\Engine and correctly bet set as template engine for view. Then it should work by {{ flash.output() }}

edited Oct '16

Show Twigengine class. Also how you added this function ? Just show TwigEngine class.

edited Oct '16

you're right, but I just wish in volt is to extend the labels {% %}

Stupid question but why even use twig ? What's a problem with volt which is much much much faster than twig and have same syntax ? If anything is missing you can easly add it to it.

Are you sure your twig engine extends Injectable ?