PHP框架_Smarty

目录

  1.环境搭建

  2.基本配置

  3.Smarty变量调节器

  4.Smarty条件判断

  5.Smarty的循环

  6.Smarty模板的引用

  7.Smarty类与对象的赋值与引用

  8.smarty函数插件

 

 

1.环境搭建

文件夹目录

|--libs      Smarty核心文件

|--demo 开发目录

  |--cache  缓存目录

  |--template_c  模板编译生成目录

  |--tpl  模板目录

  |--*.php  php文件

2.基本配置

test.php

//引入smarty类
require("../lib/Smarty.class.php");

//实例化对象
$smarty = new Smarty();

//配置Smarty
$smarty->left_delimiter = "{"; //左定界符
$smarty->right_delimiter = "}"; //右定界符
$smarty->template_dir = "tpl"; //模板目录
$smarty->compile_dir = "template_c"; //模板编译生成目录
$smarty->cache_dir = "cache"; //缓存目录

/*Smarty的缓存机制通常不开启*/
//$smarty->caching = true; //开启缓存
//$smarty->cache_lifetime = 120; //缓存时间

//smarty两个基本方法
$smarty->assign('articleTitle',"fuck the sky"); //设置title变量,赋值
$smarty->display('test.html'); //在test.html模板中展示

3.Smarty变量调节器

{* 1.首字母大写capitalize*}
{$articleTitle|capitalize}

{* 2.字符串连接 cat*}
{$articleTitle|cat:" yesterday."}

{* 3.日期格式化 date_format*}
{$yesterday|date_format}
{* %H:时 %M:分 %S:秒 %Y:年 %B:月 %e:日 %A:星期 *}
{$yesterday|date_format:"%A, %Y %B %e %H:%M:%S"}

{* 4.为未赋值或为空的变量指定默认值default*}
{$articleTitle|default:"no title"}

{* 5.转码 escape*}
{*用于html转码,url转码。默认是html转码*}

{* 6.小写 lower 大写 upper*}

{* 7.所有的换行符将被替换成<br /> nl2b*}
{{$articleTitle|nl2br}}

4.Smarty条件判断

{* eq(==) neq(!=)gt(>)lt(<)*}
{* 修饰词时必须和变量或常量用空格格开*}

{if $name eq "Ryan"}
Hello Boy.
{elseif $name eq "YY"}
NO.
{else}
Who
{/if}

5.Smarty的循环

test.php中定义$articlelist数组

$articlelist = array(
    array(
        "title" => "第一篇文章标题",
        "author" =>"第一作者",
        "content" =>"第一内容"
    ),
    array(
        "title" => "第二篇文章标题",
        "author" =>"第二作者",
        "content" =>"第二内容"
    )

);

test.html中

{* section循环 *}
{* 1.start 循环执行的初始位置。如果该值为负数,开始位置从数组的尾部算起*}
{* 2 step 该值决定循环的步长。*}
{* 3.设定循环最大执行次数。*}
{* 4.show 决定是否显示该循环。*}

{section name=article loop=$articlelist}
    {$articlelist[article].title}
    {$articlelist[article].author}
    {$articlelist[article].content}
<br/>
{/section}

{* foreach循环 *}
{foreach $articlelist as $article}
{$article.title}
{$article.author}
{$article.content}
<br/>
{foreachelse}
NULL
{/foreach}

6.Smarty模板的引用

{*include file="header.tpl" *}

7.Smarty类与对象的赋值与引用

test.php中定义myObject类

class myObject{
    function meth1($params){
        return $params[0]."  ".$params[1];
    }
}
$myObj = new myObject(); 

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

test.html

{$myObj->meth1(array("苹果","炸了"))}

8.smarty函数插件

{* function 函数插件 *}
{*创建Smarty插件:*}
{* 在插件目录里新建文件 类型.插件名.php文件*}
{* 插件方法名字书写规范: smarty_类型_插件名([...]){}*}

{* 调用方法:函数名 参数1=数值1 参数2=数值2 *}
{test width=150 height =2}

//function.test.php
function smarty_function_test($params){
    $width = $params["width"];
    $height = $params["height"];
    return $width*$height;
}
{* modifiers 修饰插件 *}
{* 调用方法:第一个参数|函数名 其他参数 *}
{$yesterday|test:"Y-m-d h:i:s"}

//modifier.test.php
function smarty_modifier_test($utime,$format){
    return date($format,$utime);    
}
{* block functions 区块函数插件 *}
{test2 replace="true" maxnum=10}
{$str}
{/test2}

//block.test2.php
function smarty_block_test2($params,$content){
    $replace = $params["replace"];
    $maxnum = $params["maxnum"];
    if($replace == "true"){
        $content = str_replace(",",".",$content);
    }
    $content = substr($content,0,$maxnum);
    return $content;
}
原文地址:https://www.cnblogs.com/Ryan344453696/p/5220658.html