开发者

what is advantage of using headlink() to insert javascript and css in zend framework?

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


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:

  • http:// framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headstyle
  • http://www.engfers.com/2008/10/30/internet-explorer-doesnt-evaluate-any-more-than-31-style-elements/ (big reason)

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜