简体   繁体   中英

Using headScript view helper for <head> and <body> scripts

I've got some scripts I'd like to add to the end of the <body> of the page, and some that I need to have in the <head> . I'm wondering if there's a more elegant way to add certain scripts to the <head> and certain in the <body> using a segment or something like that. Say I have two scripts that are going to go in the body:

$this->view->headScript()->prependFile($assetUrl . "/js/jquery.min.js");
$this->view->headScript()->appendFile($assetUrl . "/js/application.js");

And I want this one in the <head> instead:

$this->view->headScript()->prependFile($assetUrl . "/js/modernizr.min.js");

Calling $this->headScript(); outputs all three in both cases. Is there a way to group scripts? I could just paste the HTML snippet manually, but I'd like to have it in code because I switch to minified versions of the javascript if the site is running in the production environment.

I'd make my own helper called htmlScript . You should be able to extend the existing headScript helper, overriding the registry key property only.

Then just echo out your helper in your layout at the end of the document

 
 
 
  
  <?php echo $this->htmlScript() ?>
 
  

Edit Been out of the loop for too long ;)

There's already a helper for you - Zend_View_Helper_InlineScript

If you want to override the script files:

$this->view->headScript()->setFile()

EDIT I'm not sure why I got downvoted. I gave an alternative answer to your question, albeit succinctly. If you have prepended/appended two script files, but for a specific controller or module you wish to override the loading of those scripts with a third, then setFile should do exactly what you asked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM