zoukankan      html  css  js  c++  java
  • PHP smarty编程总结

           Smarty是一个使用PHP写出来的PHP模板引擎,目的是要使用PHP程序美工分离,使的程序员改变程序的逻辑内容时不会影响到美工的页面设计,美工重新修改页面时不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。(也易于程序的多样式开发)

     

    Smarty优点

           1. 速度快:相对其他模板引擎。

           2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件

           3 缓存技术:它可以将用户最终看到的HTML文件缓存成一个静态的HTML页

           4. 插件技术:smarty可以自定义插件。

     

     

    不适合使用smarty的地方

           1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新    

           2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目

     

     

    Smarty的配置

     

    smarty配置
     1 include_once("Smarty/Smarty.class.php"); //包含smarty类文件
    2 $smarty = new Smarty(); //建立smarty实例对象$smarty
    3 $smarty->config_dir="Smarty/Config_File.class.php"; // 目录变量
    4 $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
    5 $smarty->template_dir = "./templates"; //设置模板目录
    6 $smarty->compile_dir = "./templates_c"; //设置编译目录
    7 $smarty->cache_dir = "./smarty_cache"; //缓存文件夹
    8 //----------------------------------------------------
    9 //左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突
    10 //----------------------------------------------------
    11 $smarty->left_delimiter = "{";
    12 $smarty->right_delimiter = "}";

    smarty变量:

     1  
    2 $smarty->assign("模板变量", "值(数组/变量)");
    3 $smarty->display("模板名称");
    4
    5
    6 $smarty->assign("name", "PHP100中文站"); //进行模板变量替换
    7 $smarty->display("index.htm"); // 该文件就是模板文件,应该在模板目录下
    8 <html>
    9 <title>{$name}</title>
    10 ……

     

    smarty数组:

     

    $smarty->assign("模板变量", "数组");

      


     

         读取数组内容:

     

    {section name=s loop=$stu}

    {$stu[s].name}

    {sectionelse}

    无内容

    {/section}

     

     

     

    变量操作符

     

           php模板引擎smarty内置的一些操作函数,我们称之为变量操作符,变量操作符

    可用于操作变量,自定义函数和字符。(跟我PHP中常用的PHP内部函数类似)

         

            语法中使用"|"应用变量操作符,多个参数用":" 分隔开来

     

     

    常用的20个常用变量符

     

     1 capitalize [首字母大写]
    2
    3 count_characters [计算字符数]
    4
    5 cat [连接字符串]
    6
    7 count_paragraphs [计算段落数]
    8
    9 count_sentences [计算句数]
    10
    11 count_words [计算词数]
    12
    13 date_format [时间格式]
    14
    15 default [默认]
    16
    17 escape [转码]
    18
    19 indent[缩进]
    20
    21 lower[小写 ]
    22
    23 nl2br[换行符替换成<br />]
    24
    25 regex_replace[正则替换]
    26
    27 replace[替换]
    28 spacify[插空]
    29 string_format[字符串格式化]
    30
    31 strip[去除(多余空格)]
    32
    33 strip_tags[去除html标签]
    34
    35 truncate[截取]
    36
    37 upper[大写]
    38
    39 wordwrap[行宽约束]

     

    eg:

      

    1   {$name|count_characters}
    2 {$name|replace:'1':'*'}
    3

      

     

    foreach数组处理

      

           无键值数组

     

    1 {foreach from=$name item=id}
    2 id: {$id}<br>
    3 {/foreach}

      

     

          有键值数组

     

    1 {foreach key=j item=v from=$name }
    2 {$j}: {$v}<br>
    3 {/foreach}
    4

      

     

     

    include 多功能使用

         

    1 {include file="header.htm"}
    2 {include file="D:\www\head.htm"}
    3 {include file='head.htm' title="Main Menu"}

      

     

    IF条件语句的使用

       

    1 {if $name=='ok'}
    2 ...
    3 {else}
    4 ...
    5 {/if}

      

     

     

    literal strip 文本的处理技巧

     

           literal 数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用

    于显示有可能包含大括号等字符信息的 javascript 脚本

     

    1 {literal}
    2 <script language=javascript>
    3 .....
    4 </script>
    5 {/literal}

      

     

          strip标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的

    空格导致问题.

    放在html代码中,会把其中间内容的所有空格(不包括属性部分)全部去掉

     

                                                                              Smarty缓存

     

    Smarty缓存的配置

     

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

      

    Smarty缓存的使用和清除

     

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

      

     

    Smarty局部缓存

     

    insert 函数默认是不缓存的。并且这个属性不能修改。

     

    1 index.htm
    2 <div>{insert name="get_time"}</div>
    3 index.php
    4 function insert_get_time(){
    5 return date("Y-m-d H:m:s");
    6 }

      

     

     

    smarty_block 函数功能更加强大,使用方法同上

     

    1 {blockname}
    2 没有缓存的:{$smarty.now}
    3 {/blockname}

      

  • 相关阅读:
    C#使用Json
    JQuery AJAX介绍
    封装一个自己的 Ajax小框架
    AJAX跨域实现
    AJAX 一些常用方法
    完整的 AJAX 写法(支持多浏览器)
    AJAX 简单上手
    利用反射生成SQL语句
    访问Access数据库(有多个数据库时 体现多态)
    DataTable操作(建表,建行,建列,添加数据)
  • 原文地址:https://www.cnblogs.com/fanchangfa/p/2171277.html
Copyright © 2011-2022 走看看