ZendFramework - 如何从控制器添加 -> HeadScript()? [英] ZendFramework - How to add ->HeadScript() from Controllers?
问题描述
我有一个案例,我需要将控制器中的 Javascript 添加到已经有 HeadScript() 的布局中;
I have a case where i need to add the Javascript from controller to the Layout where it has already HeadScript();
如何从控制器做到这一点?
How to do that from controller?
例如:$this->view->HeadScript()->appendScript();
这是控制器:两者都不适用.
This is controller: Both does not apply.
class RouterController extends Zend_Controller_Action
{
public function init()
{
$this->view->HeadScript()->appendFile('/js/test.js')->setIndent(8);
$this->view->HeadScript( Zend_View_Helper_HeadScript::FILE, '/js/test.js' );
}
}
这是视图文件:index.phtml
This is the view file: index.phtml
<?//$this->HeadScript()->appendFile('/js/test.js')->setIndent(8);?>
如果我在视图中取消注释它可以工作但在控制器中不起作用.我想现在如何从控制器应用它?
If i uncomment in view it works but not in Controller. I want to apply this from controller how now?
推荐答案
我从控制器中的 preDispatch 方法得到了这个,记住你必须在传递标题之前传递布局更改.
I got this to work from the preDispatch method in a controller, remember you have to pass layout changes before headers are passed.
public function preDispatch() {
parent::preDispatch();
$layout = new Zend_Layout();
$layout->getView()->headScript()->appendScript('/javascript/form.js', 'text/javascript');
}
您仍然必须在布局中使用 headScript 占位符.
you still have to have the headScript placeholder in your layout.
这篇关于ZendFramework - 如何从控制器添加 -> HeadScript()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!