3

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?

e.g: $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' );
  }
}

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?

6 Answers 6

10
$this->view->headScript()->appendFile('/path/to/file.js');
Sign up to request clarification or add additional context in comments.

5 Comments

If i do it manually in the .phtml file then it does but not when i do from controllers. e.g: <?//$this->HeadScript()->appendFile('/js/namespace.extra.js')->setIndent(8);?>
@Google Also watch letter casing. HeadScript() should be headScript()
Does not work from "*************CONTROLLERS*************". (works from VIEWS).
I use this method in my controllers all the time. Perhaps you are somehow meddling with $this->view elsewhere? I.e., maybe it's not set to what you think it is?
@Google Alex wrote $this->view->headScript() not $this->headScript(). The view property of $this while your in a controller refers to the view. Note that this is true for ZF1. I think ZF2 has decoupled the view from the controller.
2
<?//$this->HeadScript()->appendFile('/js/test.js')->setIndent(8);//Your question ?>
$this->view->headScript()->appendFile('/path/to/file.js');//Alex Howansky's answer

There slightly different. :)

1 Comment

I am not trying to execute it from VIEWS.
1

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');
    }

you still have to have the headScript placeholder in your layout.

3 Comments

But that preDispatch, which results to anything if i have 100 controllers? And i want random javascript i can not do this with this.
@Google you can put a preDispatch() in every controller if you like. In fact it's already there you just rarely ever see it. So for controller level changes to layouts and such it's great.
You are correct, this was my answer. I can no apply exactly how i wanted. Thanks a lot!
1
$this->view->HeadScript( Zend_View_Helper_HeadScript::FILE, '/path/to/file.js' );

or

$this->view->HeadScript( Zend_View_Helper_HeadScript::SCRIPT, 'js code' );

The same for $this->view->InlineScript().

Comments

1
$headScript = $this->getServiceLocator()->get('viewhelpermanager')->get('headScript');
$headScript->prependFile($this->getSystemBaseUrl()."js/front_end/lib/jQuery/jquery-2.2.3.min.js","text/javascript");

1 Comment

Welcome to Stack Overflow! Could you explain how your answer addresses the problem(s) from the question? Code-only answers are not very useful, especially for further readers that stumble upon this post. Thanks!
0

In case if its still useful to someone, you need to include following in layout script file:

<?php echo $this->headScript(); ?>

Reference: https://framework.zend.com/manual/1.11/en/zend.view.helpers.html#zend.view.helpers.initial.headscript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.