views:

204

answers:

2

Documentation says to use headlink()->prependstylesheet() to insert css in the layout file. Also says to use to load the javascript files this way also. Why is this any better than just hard coding the link and script tag right in the header? I would think this is slower performance since its using the framework methods to do it. Using zend framework 1.10

+1  A: 

CSS is 'cascading'. Say you built some modularized code that's not always on the page. You'd like to change some of the default behaviors of your style without destroying any of the CSS up to that point. Well, you could inject it as the first stylesheet entry and it will serve as the base style for the rest of the stylesheets to add upon.

For Example:

* { padding: 0; margin: 0; }.

If you put that in the middle of your page, it would destroy any margin and padding that has 'cascaded' that was set for those properties on any element up to that point on you page; however, if you would have used $this->headStyle()->prependStyle("* { padding: 0; margin: 0; }"), it would have reset the margin and padding for all elements, but the rest of the style on the page would have had a chance to override those properties.

More reasons:


Why JavaScript?

  • http:// framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headscript
  • It's just good practice for modularized design. Magento eCommerce uses this method heavily

Inline JavaScript should use this:

  • http:// framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.inlinescript.

HeadScript's sibling helper, InlineScript, should be used when you wish to include scripts inline in the HTML body. Placing scripts at the end of your document is a good practice for speeding up delivery of your page, particularly when using 3rd party analytics scripts.

Hope this helps. Sorry I had to fudge the URL's because I'm a 'new' user.

engfer
ok I understand more what it is. Basically you can add style sheets in head section anywhere at anytime and you can set the order. This could be useful if you only want to load certain style sheets for certain templates or views programatically.
Andy
+1  A: 

headlink()->prependstylesheet() can by easily extended by subclassing the helper. For instance, I use custom headlink() helper to compress, obfuscate and merge all stylesheets in one file. You may have conditions in the code, which assets to load.

When you cache the output, the performance hit is not so important.

takeshin
any chance of sharing that piece of code with the rest of the world? =)
robertbasic
Sorry, not yet. Probably when I integrate it with static cache available in 1.10, I'll publish it on github.
takeshin