简体   繁体   English

使用headScript视图助手进行 <head> 和 <body> 剧本

[英]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> . 我有一些脚本要添加到页面的<body>末尾,而一些脚本需要包含在<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. 我想知道是否存在使用段或类似方法将某些脚本添加到<head>和某些脚本在<body>的更优雅的方法。 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: 我想要这个在<head>

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

Calling $this->headScript(); 调用$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. 我可以手动粘贴HTML代码段,但我想将其包含在代码中,因为如果站点在生产环境中运行,我会切换到JavaScript的精简版本。

I'd make my own helper called htmlScript . 我将创建自己的助手 htmlScript You should be able to extend the existing headScript helper, overriding the registry key property only. 您应该能够扩展现有的 headScript帮助器,仅覆盖注册表项属性。

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 已经有了您的帮手-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. 如果您已经在两个脚本文件之间添加了前缀,但是对于特定的控制器或模块,则希望使用第三个脚本文件来覆盖这些脚本的加载,那么setFile应该完全按照您的要求执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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