4

I came across the case where depending on the execution path I may need to invoke an inclusion of .js file from controller. Is there a nice way of doing it? (besides setting some view variable with actual .js include code)?

2 Answers 2

8

See the view helper headScript(). I'm just writing this off the top of my head but I think it works like this:

From within a view file: $this->headScript()->appendFile('filename.js');

From within a controller: $this->view->headScript()->appendFile('filename.js');

And then somewhere in your layout you need to echo out your headScript object:

<?=$this->headScript();?>

Sign up to request clarification or add additional context in comments.

3 Comments

While this is the correct answer, how do you handle situations where you have a set of JS files that are pre-reqs for the entire site, plus these one-offs that need to be included on certain controller pages? I find appending here causes them to appear BEFORE the "core" set is loaded...?
I think you're looking for prependFile(). You can either statically include the necessary files in your layout or call to prependFile() somewhere along the way.
yep, that's it... prepend in the header, append in the views. Thanks.
0

Sure, you could do like @Bill Karwin described.

But if you would like to do it really nicely, you need template inheritance - like it is implemented in Django framework for Python for instance. There are some extensions for Zend Framework as well, take a look at Calypso.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.