1

I have created a custom view helper and i'm calling it successfully. Only problem that i'm having is that it seems as though i'm unable to inject code into the headScript by doing the following. Everything else inside my view helper is working perfectly.

$this->view->headScript()->captureStart(); echo ' $(function() { //js code here }); '; $this->view->headScript()->captureEnd(); 

Any help would be appreciated.

Further investigation: Seems as though this is only happening on view helpers called from my layout. Calling the view helper from inside a view script works. I'm stumped!

More Investigation: echoing out the headscript like below works. anyone know why that is?

echo $this->view->headScript()->appendFile(filename) // works echo $this->view->headScript()->appendScript(script) // works 
4
  • 1
    Does your custom view helper extend Zend_View_Helper_Abstract? Commented Sep 1, 2011 at 16:16
  • @david - yes it does. Just a note, i'm calling this specific view helper inside my layout file. Would this be any different than calling it from a view script? Commented Sep 2, 2011 at 5:50
  • In the layout should be fine. For testing purposes only, maybe try calling in a view script. Are you getting an exception/error? Or just no appended js code? Can you confirm that the method containing this codeblock actually gets called? Just thinking out loud. Commented Sep 2, 2011 at 6:11
  • Is this codeblock the only part of the helper that uses the view instance? Maybe the Zend_View instance you are accessing via $this->view in the helper is not actually the same instance you bootstrapped? Commented Sep 2, 2011 at 12:27

2 Answers 2

2

Although it might be a little too late:

This probably happens because when you call the headScript() ViewHelper in your layout script below the head area of the HTML document it will not be echoed. If you echo out the result of the appendScript function the headScript() will return $this and thus echo out all remaining scripts. The effect is that your script-tag will not be in the head area of your HTML document. Instead it will be somewhere inside the body tag.

A solution might be to save the result of the view helper to a variable BEFORE the head area is echoed. Afterwards you could echo the content of that variable.

<?php $myResult = $this->myViewHelper(); ?><html> <head> <?php echo $this->headScript() ?> </head> <body> <?php echo $myResult ?> </body> </html> 
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried to use the appendScript() method?

I mean:

$this->view->headScript()->appendScript('$(function() {//js code here });', $type = 'text/javascript');) 

2 Comments

thanks for the comment, echoing your suggestion out works. No idea why i need the echo?
Sure. Because the function returns a string. So without echoing the string, nothing will be written.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.