search
Homephp教程PHP开发Package js and css files in the website

一,为什么要用smarty进行打包

apache中也有给js,css这样的静态文件进行打包压缩的模块,但是本文所说的不是以这种方式进行的打包,而是和smarty结合的方式来把网站中的js,css文件进行打包。

为什么要进行打包呢,主要目的是为了合理的管理自己的代码。现在有好多网站,你查看一下网站的源码的话,你会发现网站的头部有大量的JS文件和CSS文件,网站的尾部也有可能有大量的JS文件。以webqq为例吧:

<script type="text/javascript" src="js/util.js?20100429001"></script>
<script type="text/javascript" src="js/webeditor.js?20100429001"></script>
<script type="text/javascript" src="js/helptip.js?20100429001"></script>
<script type="text/javascript" src="js/window.js?20100429001"></script>
<script type="text/javascript" src="js/main-panel.js?20100611001"></script>
<!-- this script is for flashplayer version detection -->
<script type="text/javascript" src="js/fp_detect.js"></script>
<!--script type="text/javascript" src="js/tab-msgbox.js?20090311"></script-->
<script type="text/javascript" src="js/tab-buddystate.js?20090927001"></script>
<!--script type="text/javascript" src="js/tab-buddyimpression.js?20090311"></script-->
<script type="text/javascript" src="js/tab-addbuddy.js?20091210001"></script>
<script type="text/javascript" src="js/main.js?20100611001"></script>
<script language="javascript" src="http://pingjs.qq.com/ping.js"></script>

 上面的代码是放在webqq的底部,看着很不爽,如果只显示一个话,看着是不是很舒服呢?结果是肯定的。

  二,用一个JS文件来包涵多个JS文件

1,用function

function include(filename) {
 document.write("<script language=&#39;JavaScript&#39; type=&#39;text/javascript&#39; src=&#39;" + filename + "&#39;></script>");
}
 
include("js/jquery-1.3.2.js");
include("js/test.js");

2,用数组

var jsarray = new Array();
jsarray[0] = "js/jquery-1.3.2.js";
jsarray[1] = "js/test.js";
for(i=0;i<jsarray.length;i++){
 document.write("<script type=&#39;text/javascript&#39; src=&#39;"+jsarray[i]+"&#39;></script>");
}

三,模板文件

{foreach from=$jsArr item=js}
<script src="{$js}" type="text/javascript"></script>
{/foreach}

四,调用模板的php文件

public function addCss($<a href="http://www.php1.cn/category/72.html">css</a>) {
 if (!is_array($<a href="http://www.php1.cn/category/72.html">css</a>)) {
 $this->tpl->append(&#39;<a href="http://www.php1.cn/category/72.html">css</a>Arr&#39;, $<a href="http://www.php1.cn/category/72.html">css</a>);
 } else {
 $this->tpl->append(&#39;<a href="http://www.php1.cn/category/72.html">css</a>Arr&#39;, $<a href="http://www.php1.cn/category/72.html">css</a>, true);
 }
 }
 
 public function addJs($js) {
 if (!is_array($js)) {
 $this->tpl->append(&#39;jsArr&#39;, $js);
 } else {
 $this->tpl->append(&#39;jsArr&#39;, $js, true);
 }
 }
$this->addJs("./js/test.pkg.js");

五,总结

这样做的好处是什么呢,个人分析了以下二点:

1,把一个页面包涵的js,css文件转变成一个,代码简单

2,把这些js,css放到一个js文件里面,便于管理,如果我想加一个js,减一个js很方便,你就不用在去改模板了,如果模板多的话,你都要一个页面,一个页面去改的话,很烦人的。

如果用的不是smarty模板的话也是一样,都可以拿出来的,如果没用模板的话也是可以的,在这里只是表达一种思想。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use