码迷,mamicode.com
首页 > 其他好文 > 详细

smarty的配置实用

时间:2016-05-09 14:09:05      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

smarty是一个老牌Php模板引擎,作用是程序和美工分离(还没遇到会用smarty的 美工,还是自己整合),缓存技术减轻了服务器的处理压力。smarty缓存和页面静态化都是页面缓存技术,区别是smarty缓存是临时性的,静态化是永 久性的,可以通过组合互补达到比较好的性能优化效果。而memcache是内存对象缓存系统,与前两种比不是文件级别的缓存,而是内存级别的缓存。

 

配置:

1、在smarty官网下载最新文件包: http://www.smarty.net/解压缩文件包放到项目目录下

2、新建一个php文件初始化smarty配置:

 

技术分享
include_once("Smarty/Smarty.class.php"); //包含smarty类文件  $smarty = new Smarty(); //建立smarty实例对象$smarty  $smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量  $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存  $smarty->template_dir = "./templates"; //设置模板目录  $smarty->compile_dir = "./templates_c"; //设置编译目录  $smarty->cache_dir = "./smarty_cache"; //缓存文件夹
技术分享

 

3、在使用smarty的地方引用该配置文件

使用:

变量的使用:
php文件

 

$smarty->assign("模板变量", "值(数组/变量)"); $smarty->display("模板名称");

 

htm文件

 

<html> <title>{$name}</title>

 

 

常用变量操作符

 

技术分享
capitalize [首字母大写]  count_characters [计算字符数]  cat [连接字符串]  count_paragraphs [计算段落数] count_sentences [计算句数] count_words [计算词数] date_format [时间格式] default [默认] escape [转码] indent[缩进] lower[小写 ] nl2br[换行符替换成<br />] regex_replace[正则替换] replace[替换] spacify[插空] string_format[字符串格式化] strip[去除(多余空格)] strip_tags[去除html标签] truncate[截取] upper[大写] wordwrap[行宽约束]
技术分享

 

 

变量操作符使用语法

 

{$name|capitalize }  

 

 

 

内置函数(判断循环神马的最常用了,你懂的)
1、判断

 

{if $name==‘ok‘}  {else}  {/if}

 

2、循环

技术分享
{foreach from=$name item=id}   {$id}   {/foreach}              或          {foreach key=j item=v from=$name }   {$j}: {$v}   {/foreach}  
技术分享

3、包含(引用页面固定的头尾都会用到的啦)

 

{include file="header.htm"}

 

4、冲突处理(页面js用到大括号与smarty的大括号冲突怎么办)

 

{literal}    
    <script language=javascript>      
  </script>  
 {/literal}  

 

literal数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本

另外,strip标记处理数据的首尾空格和回车,可以避免一些浏览器兼容性问题

smarty缓存的配置:

 

$smarty->cache_dir = "/caches/";  //缓存目录  
$smarty->caching = true;  //开启缓存,为flase的时侯缓存无效  
$smarty->cache_lifetime = 60;  //缓存时间  

 

清除缓存:

$smarty->display(‘cache.tpl‘, cache_id);  //创建带ID的缓存  
$smarty->clear_all_cache(); //清除所有缓存  
$smarty->clear_cache(‘index.htm‘); //清除index.tpl的缓存   
$smarty->clear_cache(‘index.htm‘,cache_id); //清除指定id的缓存 

 

 

神马是带ID的缓存,就是同一个模板页面会显示不同的内容,需要用id区别开来,生成不同的缓存文件。想ijiefang.com里面的商家首页,都是同一个模板,但每个商家的内容都不同,需要一个商家首页一个缓存文件。

局部缓存:

 

技术分享
index.htm <div>{insert name="get_time"}</div>  
index.php function insert_get_time()
{    
   return date("Y-m-d H:m:s"); 
 或 
{blockname} 
没有缓存的:{$smarty.now}
 {/blockname}
分类: PHP

smarty的配置实用

标签:

原文地址:http://www.cnblogs.com/feng12345/p/5473524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!