![](/img/trans.png)
[英]How to add JS or CSS files to the end of the queue from the controller action in Zend Framework?
[英]How to control the js files that are load in layout, from the Controller (Zend Framework)
首先,我是Zend Framework的初學者,也許有一個簡單的方法可以做到這一點,但我不知道。
我要實現的目標是根據所使用的Controller加載不同的js文件(在布局頭中)。 我無法將js文件作為變量添加到Layout對象,因為在那些Controller中禁用了布局。
正確的做法是什么? 謝謝。
編輯(代碼)
在CalendarController中(使用Subdigger的方法):
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');
}
}
並在layout.phtml中:
<?php
echo $this->doctype()."\n";
?>
<html>
<head>
<?php
echo $this->headMeta()."\n";
echo $this->headLink()."\n";
echo $this->headTitle()."\n";
echo $this->headScript()."\n";
你可以這樣:在控制器中
class IndexController extends Zend_Controller_Action
{
public function init()
{
$this->view->headScript()->appendFile(
'/js/prototype.js',
'text/javascript',
array('conditional' => 'lt IE 7')
);
}
......
}
然后來看:
.....
<head>
<?php echo $this->headScript() ?>
</head>
.....
讀這個
我了解您的問題,但未正確理解您的意圖。 您在注釋中說,已在indexController中啟用了布局,但其他人禁用了布局。 如果您調度到另一個控制器並禁用布局,則不應使用layout.phtml。 那么,您如何使用它呢? 您是否在某個地方包含了?
我認為這里的解決方案不是禁用布局,而是在那些“其他”控制器中加載其他布局。
$this->_helper->layout->setLayout('foobaz');
您可以為每個控制器甚至是控制器中的動作加載不同的布局。 我注意到的另一件事是,您將其加載到init()方法中。 我在動作或pre / postDispatch方法中執行所有這些邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.