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='JavaScript' type='text/javascript' src='" + filename + "'></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='text/javascript' src='"+jsarray[i]+"'></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('<a href="http://www.php1.cn/category/72.html">css</a>Arr', $<a href="http://www.php1.cn/category/72.html">css</a>); } else { $this->tpl->append('<a href="http://www.php1.cn/category/72.html">css</a>Arr', $<a href="http://www.php1.cn/category/72.html">css</a>, true); } } public function addJs($js) { if (!is_array($js)) { $this->tpl->append('jsArr', $js); } else { $this->tpl->append('jsArr', $js, true); } } $this->addJs("./js/test.pkg.js");
五,总结
这样做的好处是什么呢,个人分析了以下二点:
1,把一个页面包涵的js,css文件转变成一个,代码简单
2,把这些js,css放到一个js文件里面,便于管理,如果我想加一个js,减一个js很方便,你就不用在去改模板了,如果模板多的话,你都要一个页面,一个页面去改的话,很烦人的。
如果用的不是smarty模板的话也是一样,都可以拿出来的,如果没用模板的话也是可以的,在这里只是表达一种思想。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
