Once I’ve posted about Zend Framework and script injections into the view of the app. However back than I didn’t mentioned the way the scripts can be injected on whatever place into the markup. This job’s done by Zend_View_Helper_InlineScript and that’s only an abstraction over the HeadScript.
However thus the PHP code goes quite clean and maintainable.
$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js'); |
$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js');
This is quite interesting because in the case of multiple scripts you can chain them into this:
$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js') |
$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js')
But that’s not everything. Although it looks very pretty and clean, PHP gives you the correct syntax of something like this:
$scripts = $this->view->inlineScript();
$scripts
->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js') |
$scripts = $this->view->inlineScript();
$scripts
->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js')
And that’s particularly good when it comes to fast switching between production and development scripts includes.
$scripts = $this->view->inlineScript();
$scripts
// ->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js') |
$scripts = $this->view->inlineScript();
$scripts
// ->appendFile('/scripts/production.js')
->appendFile('/scripts/development.js')
You’d ask why I’d to comment my script includes. Because as it appears to be fashionable the JavaScripts are concatenated and compressed. This gives you performance benefits on the client side when downloading and executing the script. So it’s usual to have one compressed/minified and several development scripts. That’s why this commenting strategy is very useful.