zoukankan      html  css  js  c++  java
  • 转 smarty学习笔记电子版整理

    使用smarty模版,smarty类的文件在libs目录下,将libs目录拷贝到工作区当前目录下,建立templates和templates_c文件夹,也可以通过代码自己指定,推荐用系统默认目录。

    require_once("../Smarty-3.1.15/libs/Smarty.class.php");  

    $smarty=new Smarty;

    $smarty->left_delimiter="<{"; //设定smarty变量左标记,默认为{

    $smarty->right_delimiter="}>"; //设定smarty变量右标记,默认为}

    分配变量

    $smarty->assign(“title”,”标题”);

    在模版中读取分配的内容<{title}>

    程序中的数组

    $arr5=array(

    "emp1"=>array("id"=>"001","name"=>"xiaoming","age"=>"19"),

    "emp2"=>array("id"=>"002","name"=>"daming","age"=>"15")

    );

    $smarty->assign("arr5",$arr5);

    模版中数组读取

    <{$arr5.emp1.name}>

    程序中的对象

    class Dog{

    var $name;

    var $age;

    var $color;

    function __construct($name,$age,$color){

    $this->name=$name;

    $this->age=$age;

    $this->color=$color;

    }

    }

    $dog1=new Dog("小朵","5","red");

    $smarty->assign("dog1",$dog1);

    模版中对象读取

    <{$dog1->name}>

    配置文件config/my.conf

    title="我的第一个配置文件";

    bgcolor="pink";

    模版中读取配置文件

    <h1><{#title#}></h1><br />

    程序中的二维下标数组

    $arr3=array(

    array("河北","吉林","中州"),

    array("财神","萨卤","贝清")

    );

    模版中循环读出二维数组中的数据,其中key是下标索引,外围的temp代表了一维数组,item代表数组中元素的内容

    {foreach from=$arr3 item=temp key=k}

    {foreach from=$temp item=val}

    {$val}

    {/foreach}

    {/foreach}

    Smarty直接获取值

    <{$smarty.get.username}>//直接获取get方式传来的username的值

    <{$smarty.server.SERVER_NAME}>//获取ip服务器名

    自定义函数的注册

    function test($args){

    $str="";

    for($i=0;$i<$args['time'];$i++){

    $str.="<font size='".$args['size']

    ."'color='".$args['color']."'>"

    .$args['content']." "."</font>";

    }

    return $str;

    }

    //注册函数

    $smarty->registerPlugin("function","hsp","test");

    在模版中调用自定义函数

    <h1>自定义函数的使用</h1>

    {hsp time="4" size="5" content="自定义函数的调用" color="blue"}

    自定义块的注册

    function test2($args,$con){

    $str="";

    for($i=0;$i<$args['time'];$i++){

    $str.="<font size='".$args['size']

    ."'color='".$args['color']."'>"

    .$con." "."</font>";

    }

    return $str;

    }

    $smarty->registerPlugin("block","blk","test2");

    在模版中调用块

    <h1>块调用</h1>

    {blk time="4" size="5" color="blue"}

    块的调用测试

    {/blk}

    Smarty插件,smarty插件在libs/plugins/目录下

    函数插件的格式为function.hlp.php,块名block.blk.php,即function.函数名.php,block.块名.php

    文件中程序代码格式为

    smarty_function_hlp($args,&$smarty){}或块名smarty_block_blk($args,$con,&$smarty)

    &$smarty是必要的格式,$con是模版中闭合标签的内容

    变量调节器,连接字符串cat,首字母大写capitalize

    {$varname | cat:”yez”:”second” | capitalize}//连接首字母并大写

    {$eshtml|escape}//将html以普通文本输出

    调节器插件modifer.xxx.php,程序函数名为smarty_modifer_xxx($string)

    验证邮箱格式是否合法调节器

    <?php

    function smarty_modifier_isemail($str){

    $pattern="/^[a-zA-Z_][a-zA-Z0-9_-]*@[a-zA-Z0-9]+(.[a-zA-Z0-9]+)+$/i";

    if(preg_match($pattern,$str)==1){

    return "是合法的格式";

    }else{

    return "邮箱格式不合法";

    }

    }

    ?>

    在模版中使用

    <{$varname | isemail}>

    preg_match($pattern,$str,$arrs);//得到的结果是一维数组

    preg_match_all($pattern,$str,$arrs);//得到的结果是二维数组

    {include file=”bottom.tpl” varname=”所传递的值”}

    在bottom.tpl中{$varname}可获得传来的值

    $smarty->caching=true;//开启缓存

    $smarty->cache_lifetime=60;//设置缓存时间为60秒

    $smarty->debugging=true;//开启调试台控制窗口

    局部的代码实时更新

    function insert_mytime(){return date(“Y-m-d h:i:s”);}

    模版中使用{insert name=mytime}

    设置编号的缓存

    $smarty->display(“test.tpl”,”11”);//设置编号为11的缓存

    $smarty->clearAllCache();//删除所有缓存

    $smarty->clearCache(“test.tpl”,”11”);//删除指定编号的缓存

     

  • 相关阅读:
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第4题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第3题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第2题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第1题
    二分匹配题集
    004 IOC---IOC容器
    003 简单使用spring
    002 IOC--Bean创建的分析
    001 spring介绍
    012 shiro的jsp标签
  • 原文地址:https://www.cnblogs.com/qilinge/p/5351825.html
Copyright © 2011-2022 走看看