1

Zend expressive - Layout

https://github.com/zendframework/zend-expressive-twigrenderer/issues/24

Based on this question, I want to pass a variable to layout from an Action How can I try to do that?

$toast = [
    'level'=>'info',
    'msg'  =>'rafael',
    'url'  => null
];

$this->template->addDefaultParam(Template\TemplateRendererInterface::TEMPLATE_ALL,'toast',$this->toastrMessenger->show($toast));

return new HtmlResponse($this->template->render('contentpages::contact',$data));

on my layout/default.phtml

<?php 
if ( isset($this->toast) ){
echo $this->toast;    
}
?>

</body>
</html>
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49

1 Answers1

1

Did you try "addDefaultParam" method? signeture is;

public function addDefaultParam($templateName, $param, $value)

and you can set star (*) as $templateName (see TemplateRendererInterface::TEMPLATE_ALL) so i think layout can read it.

You can read about it on documentation. Let me know if its working.

  • but this is on my question. look. – Rafael Guimarães Nov 29 '17 at 16:53
  • yep it's. sorry silly me. i just checked zend-view renderer for expressive. on line 325 "prepareLayout" method prepares layout and it does not uses any variable. so "addDefaultParam" method does not affect layout. Basicly you have 2 options at this point. Extend yout renderer and send variables to layout (check edit), 2nd create a view helper. i'm sure you can find lots of example about it. – Mehmet SÖĞÜNMEZ Nov 30 '17 at 08:26
  • never mind extending renderer. atleast zend-view-rendere because "prepareLayout" is private method. I think you should create a view helper. – Mehmet SÖĞÜNMEZ Nov 30 '17 at 08:35
  • Also you can check this: https://github.com/zendframework/zend-expressive-zendviewrenderer/issues/21 @mtymek's comment. – Mehmet SÖĞÜNMEZ Nov 30 '17 at 08:36
  • Thanks! I will look into this. – Rafael Guimarães Dec 01 '17 at 16:38