CakePHP : Difference between " $this->fetch('css') & $this->Html->css('cake.generic') in cake php

Asfaq Tamim picture Asfaq Tamim · Dec 19, 2013 · Viewed 7.9k times · Source

Can Someone please Explain this two line used in CakePHP layout.ctp file. It seems both line used for adding css .Then what is the difference ? How they works.

 $this->Html->css('cake.generic')
 $this->fetch('css') 

Answer

SW4 picture SW4 · Dec 19, 2013

See the documentation on using blocks for script and CSS files.

In your view(s) you can use: $this->Html->css('cake.generic')

In your layout(s), you can use: $this->fetch('css')

A layout contains presentation code that wraps around a view. Anything you want to see in all of your views should be placed in a layout.

What this means is that when defining your layout, using $this->fetch('css') will add in any css blocks used in your views, so if in your views you'd put both:

$this->Html->css('cake.generic') $this->Html->css('cake.special')

Using $this->fetch('css') in your layout would include both. Note that without defining the block content first, it will not call anything. The addition of $this->Html->css('cake.generic') in the layout file before the block call ensures it is added if it is not already included in a defined view.