In my layout I am using bunch of JavaScripts. Two of these scripts are conditional and they should only be loaded for IE9. And the remaining one should be loaded for all browsers.
To achieve this I have done following in my Bootstrap class.
$this->_view->headScript ()->appendFile ( '//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js', 'text/javascript', array('conditional' => 'lt IE 9'));
$this->_view->headScript ()->appendFile ( '//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9') );
$this->_view->headScript ()->appendFile ( $this->_view->baseUrl ( '/assets/js/scripts.min.js?ver=1.0.0' ) );
This works and I get following output in my layout.
<!--[if lt IE 9]> <script type="text/javascript" src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script><![endif]-->
<!--[if lt IE 9]> <script type="text/javascript" src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script><![endif]-->
<script type="text/javascript" src="http://om.localhost.com/assets/js/scripts.min.js?ver=1.0.0"></script>
The output above is fine. But its recommended that I put the conditional scripts in the head section of the layout and the remaining scripts at the end of the body.
Is there a way to achieve this in ZF1?
P.S. I've tried using placeholders but it didn't worked. And I don't want to hard code these scripts in the layout.
Thanks for your help.
To the head section, you can use headScript()
method like your code.
To the end of the body, you can use InlineScript()
method like this:
$this->view->InlineScript()->appendFile($this->view->baseUrl() .'/js/myfile.js');
and at the end of your layout add:
<?php
echo $this->InlineScript();
?>
To add script in the bootstrap, you can do somehing like this:
protected function _initView()
{
$options = $this->getOptions();
if (isset($options['resources']['view'])){
$view = new Zend_View($options['resources']['view']);
}
else{
$view = new Zend_View;
}
$view->InlineScript()->appendFile(...);
}