Appending scripts from view helper method not working in zend framework
I have a view helper method which is like this
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
public function loginForm()
{
$script = "<script type='text/javascript'>(function (){ $('#submit').click(function (){alert('hello'); return false;})})</script>";
$this->view->headScript()->appendScript($script, $type = 'text/javascript');
$login = new Application_Form_User();
return $login;
}
}
But this is not working. I also tried
$this->view->headScript()->appendFile($this->view->baseUrl('/js/jquery.js'), 'text/javascript');
but this is not working either. If i try this code in layout.ph开发者_运维百科tml then it works.Any Idea?
In view file:
<?php $this->headScript()->appendFile('your/sript/file.js') ?>
In your layout:
<?php echo $this->headScript() ?>
You have to add setView method:
class My_View_Helper_ScriptPath { public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function scriptPath($script)
{
return $this->view->getScriptPath($script);
}
}
精彩评论