php - CakePHP: Advantages of adding CSS to CSS block instead of using inline output when using HtmlHelper? -
i started learn cakephp few days ago, following blog tutorial.
in process of writing small project myself familiar framework.
having studied documentation, noticed there 2 ways include css files.
1 way echo link tag(s) using htmlhelper: echo $this->html->css(array('style', 'forms', 'modal'));
. type of linking referred 'inline style' according options array.
the other method add tags (i believe default?) css block , print block inside <head>
:
echo $this->html->css(array('style', 'forms', 'modal'), array('inline' => false)); echo $this->fetch('css');
what advantages of using 1 way on other?
consider following layout file:
... <head> ... <?= $this->html->css('main.css'); ?> <?= $this->fetch('css'); ?> ... </head> ...
the simplest way
by default rendered view contain:
... <head> ... <link rel="stylesheet" type="text/css" href="/css/main.css" /> </head>
if there no logic associated whether css file should added - it's appropriate edit layout file , add css file, ignoring inline
property.
advantage: it's simple, clear , obvious what's happening
the dynamic way
if there logic associated whether particular css file should included - inline
property becomes useful.
consider following view file:
<?php if ($something) { $this->html->css('maps.css', ['inline' => false]); echo $this->element('maps'); } ?> view contents
or plugin includes following helper:
<?php class awesomehelper extends apphelper { public function beforelayout() { $this->html->css('awesome.css', ['inline' => false]); $this->html->js('awesome.js', ['inline' => false]); // applies js files } }
in these cases without using inline property or editing layout file, it's not possible add css files head of rendered output. using inline property, possible build css files required final view.
advantage: code outside layout file can add required css files output in head.
Comments
Post a Comment