Avoid repeatetive code in view template


#1

Hi guys, just a quick question. :smile: is there a way to extend a view template in AppView class?
I’m looking for a solution on not repeating any code that I put in my template.ctp file that looks like this:

$this->extend(’…/Layout/TwitterBootstrap/dashboard’);

and want to move it to AppView so I could just define like, if ($this->requests->controller) are Pages, Dealers, or whatever so I could use that extend statement there…


#2

Elements would not serve what you need?

https://book.cakephp.org/3.0/en/views.html#elements


#3

Yep, that might be the best way to go.

Some suggestions from cakephp gurus at Slack.

admad [7:52 PM]
@Neil you can try overriding View::_evaluate()  and adding the `extend()` call before `include` statement

admad [7:56 PM]
but thats hackish and you could end up with a loop if not careful
so will need proper checks to decide when to extend

Looking into cells too. :smiley:

https://book.cakephp.org/3.0/en/views/cells.html