4

In their new documentation there isn't anything for flash messages.

I installed the flash extension from their github repository (slimphp/Slim-Flash). Everything works fine, I can add messages and can also get these messages.

// Adding a message
$this->flash->addMessage('test', 'This is a message');

// Getting a message
$this->flash->getMessage('test')[0];

But this only works inside routes. Of course I want to have these messages displayed on my view.

But I just don't know how to get this message on the twig view.

I have already tried:

{{ container.flash.message('test')[0] }}
{{ container.flash.getMessage('test')[0] }}
{{ this.flash.message('test')[0] }}
{{ this.flash.getMessage('test')[0] }}
{{ flash.message('test')[0] }}
{{ flash.getMessage('test')[0] }}
{{ app.flash.message('test')[0] }}
{{ app.flash.getMessage('test')[0] }}
{{ container.flash.test }}

Thanks for help!

akmozo
  • 9,829
  • 3
  • 28
  • 44
deflox
  • 103
  • 2
  • 8

1 Answers1

3

You can add the flash message inside the data attribute from the render method:

$this->view->render($res, 'path/to/template.twig', [
    'flash' => $this->flash
]);

Or you could add a middleware and add your flash instance to the twig parameters

$app->add(function ($request, $response, $next) {
    $this->view->offsetSet("flash", $this->flash);
    return $next($request, $response);
});

then it should be possible to access the messages inside the twig template with

{{ flash.getMessage('test') }}

jmattheis
  • 10,494
  • 11
  • 46
  • 58