Manage JavaScript and CSS includes within Zend Framework application

How to include JavaScript and CSS files?

In Zend Framework there is a very elegant way to include CSS and JavaScript. Because in one single view of any action you cannot include a JavaScript or CSS because they wont be executed from the browser, you can simply use a helper.

You can see the original source of how that’s done here. And in breve you can include a JS file like so:

<? $this->headScript()->appendFile('/media/js/global.js') ?>

but where the problem is?

As a very good PHP point of view, there is really that way to include JS into the application. But a PHP point of view is not a JavaScript point of view so this solution is not the best one.

As almost everybody knows JavaScript blocks other content until it’s loaded and executed from the server and nowadays the most popular way to include JS is just before the closing <body> tag.

That’s why the ZF solution is not optimal. What we actually need as both PHP and JavaScript developers is a most robust solution!

3 thoughts on “Manage JavaScript and CSS includes within Zend Framework application

  1. There is another view helper for inline scripts that I use for placing my scripts right before the tag:

    inlineScript()->appendFile($this->baseUrl(‘/media/js/global.js’)) ?>

    Then, in the layout:

    inlineScript(); ?>

  2. Hi, with this helper you can put wherever you want the script tag. You define in your layout where you want to print it, in the header section o before the closing body tag. Maybe the helper’s name is misunderstood, headerscript doesn’t mean that, obligatory, it has to be in the header section.

Leave a Reply

Your email address will not be published. Required fields are marked *