• smarty的配置实用


    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
  • 相关阅读:
    initializer_list形参
    前置递增运算符和后置递增运算符的区别
    基本的TCP socket API
    C++ find()函数
    python-对目录下的文件按时间排序
    js常用方法
    selenium中的对文本进行全选,复制,粘贴,剪切和删除的操作
    python 打包exe 命令及去除 cmd框
    mysql命令行修改密码
    html文件转换成excel
  • 原文地址:https://www.cnblogs.com/feng12345/p/5473524.html
Copyright © 2020-2023  润新知