Smarty模板引擎技术(二)

一、使用Smarty模板引擎步骤

  第一步:引入smarty核心类文件并创建一个smarty实例对象:
    include_once “libs/smarty/Smarty.class.php”;
    $smarty=new Smarty();

  第二步:对核心类中属性或变量的配置
    $smarty->template_dir=”./templates”;
    $smarty->compile_dir=”./templates_c”;
    $smarty->config_dir=”./configs”;
    $smarty->caching=false;
    $smarty->cache_dir=”./cache”;
    $smarty->left_delimiter=”{”;
    $smarty->right_delimiter=”}”;

  第三步:给模版变量赋值
    $smarty->assign(“模版变量”,”值(变量|数组)”);
    比如:$smarty->assign(“username”,”张三”);

  第四步:显示对应的模版文件
    $smarty->display(“模版文件名”);
    比如:$smarty->display(“index.html”);

  第五步:在模版文件中使用smarty的左右边界符将模版变量显示出来
  <html>
    <head></head>
    <body>
      用户名:{$username}
    </body>
  </html>

二、Smarty应用之模版变量:分三种来源

  (I) 从PHP中分配的变量  

    1. 格式:$smarty->assign(“模版变量”,”值(变量|数组)”);
    2. 例如:$username=”小三”;
        //将php变量分配给模版变量
        $smarty->assign(“username”,$username);
    3. 在模版文件中通过左右边界符来显示模版变量
        例如:{$username}

  (II){$smarty}保留变量

    1.使用保留变量可访问页面请求变量:get、post、server、request、session等
    例如:页面请求变量:
        {$smarty.get.username}         //相当于在php文件中访问$_GET[‘username’]
        {$smarty.post.username}         //相当于在php文件中访问$_POST[‘username’]
        {$smarty.session.username}      //相当于在php文件中访问
        $_SESSION[‘username’]
        {$smarty.cookie.username}        //相当于在php文件中访问$_COOKIE[‘username’]
        {$smarty.request.username}        //相当于在php文件中访问
        $_REQUEST[‘username’]
        {$smarty.server.REMOTE_ADDR}      //相当于在php文件中访问
        $_SERVER[‘REMOTE_ADDR’]

    2.使用保留变量可访问常量:const
        {$smarty.const.__FILE__}
    3.使用保留变量可读取配置文件的变量:config
        {$smarty.config.username}
      读取配置文件中的变量
        1. 配置文件应放在$smarty->config_dir=”./configs”指定的目录下面
        2. 配置文件的后缀名为 .conf
        3. 配置文件:myself.conf
          #全局配置变量
            title=“调用配置文件”
            bgcolor=“red”
            border=5
            type=“计算机”
            name=“php从入门到精通”
          #局部变量
            [login]
            username=“用户名”
            password=“密 码”

        4. 一个配置变量占一行,并且后面没有以分号结尾

  (III)模版中引用配置文件中的变量

    首先要加载配置文件:
      //加载配置文件,但是只能将全局变量加载进来
        {config_load file=“my.conf”}
      //如果要加载局部变量,需要使用section属性指定特定的节
        {config_load file=“my.conf” section=“login”}

    模版文件中引用配置文件中变量的方法:
      //第一种:需要使用两个#
        比如:{#title#}
      //使用{$smarty}保留变量
        比如:{$smarty.config.bgcolor}

三、Smarty变量操作符或变量调节器

  什么是smarty的变量操作符?
     smarty模版引擎中有内置的一些函数,这些函数主要用来修饰或调节变量,我们称之为变量操作符或变量调节器,变量操作符主要用于操作变量,它可以帮助我们完成许多比较实用的功能,比如:首字母大写、拆分、替换、截取等等,这些功能通过smarty模版引擎可以很容易的在模版文件中实现,而不必再在php文件中通过php的内置函数去处理,这样的话就减少了php的代码量

  如何使用smarty的变量操作符?
    1.语法中使用 “|” 来应用变量操作符,多个参数用 ”:” 分隔开来
      比如: 在模版文件中使用变量操作符截取前十个字符
        {$name|truncate:10}

    2.常见的smarty变量操作符
        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[行宽约束]
  3.使用smarty变量操作符的例子
    例如:
      日期:{$smarty.new|date_format:”%Y-%m-%d”}
      变量中的字符数(包括空格):{$str|count_characters:true}
      多个变量操作符修饰一个变量:{$str|nl2br|upper|cat:”张三”}
      模版变量+“张三”:{$str1|cat:”张三”}
      缩进四个空白字符,并使用*来代替:{$str2|indent:4:”*”}
      把变量中的”hello”替换为“world”:{$str3|replace:”hello”,”world”}

原文地址:https://www.cnblogs.com/yexiang520/p/5751043.html