Zend 框架 - 如何在 header 中仅添加一次 CSS 和 JS 而不会重复?

Zend framework - how to add CSS and JS only one time in the header without duplication?

在 ZF2 中,我尝试只加载一次 CSS 和 JS 文件。

但是当我呈现页面时,它们被加载了两次或三次,导致网站速度极慢。

真实页面我有bootstrap.css2次,style.css2次,JS文件也有2、3次

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />

  <?= $this->headTitle(); ?>  
  <?= $this->headMeta(); ?>

  <!-- CSS -->
  <?= $this->headLink()->prependStylesheet('/v2/css/bootstrap.css'); ?>  
  <?= $this->headLink()->prependStylesheet('/v2/css/style.css'); ?>  

  <!-- JS -->
  <?= $this->headScript()->prependFile('/v2/js/jquery-3.1.0.min.js') ?>
  <?= $this->headScript()->prependFile('/v2/js/global.js') ?>

</head>

你在每一行都回应了这个对象;删除所有 <?= 并仅替换为对 echo 的一次调用。

<head>
  <meta charset="utf-8" />

  <?= $this->headTitle(); ?>  
  <?= $this->headMeta(); ?>

  <!-- CSS -->
  <?php 
    $headLink = $this-headLink();
    $headScript = $this->headScript();

    $headLink->prependStylesheet('/v2/css/bootstrap.css');
    $headLink->prependStylesheet('/v2/css/style.css');

    $headScript->prependFile('/v2/js/jquery-3.1.0.min.js');
    $headScript->prependFile('/v2/js/global.js');

    echo $headLink;
    echo $headScript;
  ?>
</head>

最简单的方法是链接它,像这样:

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />

  <?= $this->headTitle(); ?>  
  <?= $this->headMeta(); ?>

  <!-- CSS -->
  <?= $this->headLink()->prependStylesheet('/v2/css/bootstrap.css')
        ->prependStylesheet('/v2/css/style.css'); ?>  

  <!-- JS -->
  <?= $this->headScript()->prependFile('/v2/js/jquery-3.1.0.min.js')
        ->prependFile('/v2/js/global.js'); ?>
</head>

这就是 ZendSkeleton layout 中的例子。