I'm working with a declarative JavaScript component library and trying to integrate that into the cake admin views and templates, specifically for form elements.

The trouble I'm having is that I need to pass a value from my Template or View to the Layout. I can't find any documentation about how to achieve this in CakePHP 3.x.

In CakePHP 2.x it was easy, you just had to set the $this->var_for_layout.

2 Answers

ADmad On Best Solutions

You can use $this->set() in your template to set additional view vars and those will be available in your layout same why the view vars set in controller are.

If you want to set HTML fragments from template for the layout you can also use View Blocks.

David Yell On

You don't, can't and shouldn't be setting variables for a Layout in a Template.

If you need data in either a layout or a template, you'll need to set it in your application and pass it to the view layer.

If you need data in the Layout, this is usually gathered and set in the AppController so that it's always available to the Layout regardless of your controller action.

Most people use the AppController::beforeFilter method.