优化你的smarty

来源:互联网 发布:sql server教程 编辑:程序博客网 时间:2024/04/29 04:44

优化你的smarty

此文章主要针对在使用中,在不改变smarty代码的基础上,增加一些我们自己的东西,而达到一些优化而写的.主要是我在使用中遇到一些问题的解决方法,希望对大家有用~ : )

一、

smarty默认的方式是compile_check = true;即在每次输出模板的时候检查当前模板是否有过改变,如果有那么重新编译(判断时间戳),这会浪费一些效率,但会保证模板改动后可以正常显示,但在我们的网站上线后,很多时候,是不需要检查这个的,因为模板已经不变了~

我的建议:在开发阶段,force_compile = true;产品上线时compile_check = false;

类似下面的代码:

 

<?php if (DEBUG) {    $smarty->force_compile = true;} else {    $smarty->compile_check = false;} ?>

 

 

二、

smarty在处理include的时候,是在主页面中做include操作,这样会有一个问题,就是当include标签越多的时候,造成页面运行慢,我的改造是合并所有的include标签,这样不必改变使用习惯,而在编译的时候smarty相当于编译了一个页面,不必执行是再include其他的小页面,当然,也许这只是试用于类似我这样,include只作为引用公共页面的功能,所以你可以自己抉择~;)

要说明的是,这样做得缺点是smarty无法检查include页面的变动,并自动重新编译,你需要自己来进行清空编译文件目录,或者是变动主模板页面来,让smarty重新编译模板~

下面是我的,做法,聪明的你一定看得懂~

 

<?php $smarty->register_prefilter('smarty_fix_include');function smarty_fix_include($out_put, &$smarty){    // 找出所有的include标签,并找出file    return preg_replace_callback('//{include/s+.*?file=[/'"]*(.+)[/'"]*/}/i', 'smarty_fix_include_callback', $out_put);} function smarty_fix_include_callback($match){    $smarty = getInstance('Smarty');    $file = $smarty->template_dir . '/' . $match[1];    return file_get_contents($file);} ?>

 

三、

现在我们要做的是将所有的css提到header里面,类似的方法你可以将某些js扔到页面下面,并且压缩他们,这里我们用到了 csstidy 和 JavaScriptPacker.

代码如下:

// 前置link
if (preg_match_all('/<link.*?href=[/'"]*([^/'"]+)[/'"]*.*?//>/’, $out_put, $regs))
{
$tidy = new csstidy();
$tidy->set_cfg(’remove_last_;’, true);
$tidy->load_template(’highest_compression’);
$result = array_unique($regs[0]);
$all = implode(”, $result);
foreach ($regs[1] as $key => $val)
{
   if (strpos($val, ‘.css’) !== false)
   {
    $dir = dirname($val);
    $name = basename($val, ‘.css’);
    $css_code = file_get_contents(ROOT_PATH . ‘/’ . $val);
    $tidy->parse($css_code);
    if (file_put_contents(ROOT_PATH . ‘/’ . $dir . ‘/’ . $name . ‘_tidy.css’, $tidy->print->plain()))
    {
     $all = str_replace($val, $dir . ‘/’ . $name . ‘_tidy.css’, $all);
    }
   }
}
if (strrpos($out_put, ‘‘) !== false)
{
   $out_put = str_replace($result, ”, $out_put);
   $out_put = strtr($out_put, array(’‘ => $all . “/n“));
}
}
// 压缩js
if (preg_match_all(‘%<script.*?src=[/'"]*(?!http:)([^/'"]+)[/'"]*.*?></script>%’,$out_put, $regs))
// 这里做了一些处理,只匹配非http开头的script标签
{
foreach ($regs as $key => $val)
{
   $regs[$key] = array_unique($val);
}
foreach ($regs[0] as $key => $val)
{
   $dir = dirname($regs[1][$key]);
   $name = basename($regs[1][$key], ‘.js’);
   $js_code = file_get_contents(ROOT_PATH . ‘/’ . $regs[1][$key]);
   $pack = new JavaScriptPacker($js_code);
   if (file_put_contents(ROOT_PATH . ‘/’ . $dir . ‘/’ . $name . ‘.pack.js’, $pack->pack()))
   {
    $out_put = str_replace($regs[1][$key], $dir . ‘/’ . $name . ‘.pack.js’, $out_put);
   }
}
}

四、

在smarty编译模板前,我们可以用下面的方式去掉页面的注释,并对html进行压缩,以减少页面输出体积。

类似下面的代码:

$smarty->register_prefilter('smarty_prefilter');
function smarty_prefilter($out_put, &$smarty)
{
    //保护smarty标签将<-- {xxx} -->替换成 {xxx}
    $out_put = preg_replace('/<!--[^>|/n]*?({.+?})[^<|{|/n]*?–>/’, ‘/1′, $out_put);
    // 去掉html注释
    $out_put = preg_replace(’/<!–.*?–>/’, ”, $out_put);
    // 压缩html代码,去掉行首尾空格及换行
    return preg_replace(”//s*[/n/r]+/s*/”, ”,$out_put);
}

 

五、总结

经过前面讲到的处理方法,可以总结针对smarty优化的方法,整理一些,方便大家理清思路,做相应的处理,如果有疑问,请回看之前的文章~

注意:我提到的优化方式,主要是针对smarty编译模板前,对模板进行处理,即用到smarty的register_prefilter函数,来达到预处理的目的,这样做的好处是处理只在编译模板时进行.

1. 在开发阶段结束后,网站上线后,关掉compile_check,即设置:compile_check = false;

2. 处理include标签,将多个小页面合并成完整的页面在进入编译.

3. 将css文件提前,可能的话将js文件放到页面下面,并压缩他们,(如果可以,你还可以合并他们)

4. 去掉页面注释,并压缩html代码.gzip(deflate甚至是:bzip2)输出页面,当然这也适合css,js文件的输出.注意是判断浏览器是否支持他们.

经过这一番改造,相信你的页面输出会更快~

 ---★ 本文转摘自『IT学习者』→ http://www.itlearner.com/article/4552

原创粉丝点击