How to control the js files that are load in layout, from the Controller (Zend Framework)
First of all, I'm a beginner in Zend Framework and maybe there is a simple way of doing this but i don't know it.
What I am trying to achieve is to load different js files (in layout head) depending on what Controller is used. I can't add the js files as variables to the Layout object because the layout is disabled in those Controllers.
What's the proper way to do it? Thank you.
Edit (the code)
In the CalendarController (using Subdigger's method):
public function init()
{
$this->_helper->layout->disableLayout();
$js = new Application_View_Helper_Javascript();
//get an array with the basename of the js files
$jsFiles = $js->addFiles('calendar');
foreach ($jsFiles as $k=>$file){
$this->view->headScript()->appendFile('/js/' . $file.'.js');
}
}
And in the layout.phtml:
<?php
echo $this->doctype()."\n";
?>
<html>
<head>
<?ph开发者_开发百科p
echo $this->headMeta()."\n";
echo $this->headLink()."\n";
echo $this->headTitle()."\n";
echo $this->headScript()."\n";
you can go by this way: in controller
class IndexController extends Zend_Controller_Action
{
public function init()
{
$this->view->headScript()->appendFile(
'/js/prototype.js',
'text/javascript',
array('conditional' => 'lt IE 7')
);
}
......
}
then in view:
.....
<head>
<?php echo $this->headScript() ?>
</head>
.....
read this
I understand your problem but not your intentions correctly. You say in a comment that the layout is enabled in the indexController but the others have the layout disabled. If you dispatch to another controller and disable the layout the layout.phtml should not be used. So, how do you use it? Do you have an include somewhere?
I think the solution here is not to disable the layout but to load a different layout in those "other" controllers.
$this->_helper->layout->setLayout('foobaz');
You can load a different layout for each controller or even action in a controller. Another thing I noticed is that you load it in the init() method. I do all this logic in the actions or pre/postDispatch methods.
精彩评论