在ZF2,我试图加载CSS和JS文件只有一次。
但是当我渲染页面时,它们被加载了两到三次,导致网站非常慢。
在实际页面我有bootstrap.css 2次,style.css 2次,和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>