PHP smarty快速入门

smarty安装

composer下载smarty

composer require smarty/smarty

导入smarty,并创建smarty对象

//引入Smarty.class.php
require_once(‘../vendor/smarty/smarty/libs/Smarty.class.php‘);

$smarty = new Smarty();

smarty基础目录配置

$smarty->setTemplateDir(‘./templates/‘);
$smarty->setCompileDir(‘./templates_c/‘);
$smarty->setConfigDir(‘./configs/‘);
$smarty->setCacheDir(‘./cache/‘);

smarty分配变量

$name = ‘smarty‘;
$smarty->assign(‘name‘,$name);

tpl模板使用变量

{$name}

smarty 调用模板

$smarty->display(‘index.tpl‘);

smarty 封装配置

/SmartySetup.php

<?php
//引入Smarty.class.php
require_once(‘../vendor/smarty/smarty/libs/Smarty.class.php‘);


class SmartySetup extends Smarty
{

    public function __construct()
    {
        parent::__construct();

        $this->setTemplateDir(‘./templates/‘);
        $this->setCompileDir(‘./templates_c/‘);
        $this->setConfigDir(‘./configs/‘);
        $this->setCacheDir(‘./cache/‘);

        $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
    }
}

/index.php使用

<?php
//引入Smarty.class.php
require_once(‘./SmartySetup.php‘);

$smarty = new SmartySetup();

$name = ‘smarty‘;

$smarty->assign(‘name‘,$name);
$smarty->display(‘index.tpl‘);

基本语法

注释
{* 这是一个注释 *}
<!-- HTML 注释 -->
变量

index.php

$smarty = new SmartySetup();
    $name = ‘smarty‘;
    $array = [1,2,3];
    $array2 = [
        ‘name‘=>‘array_smarty‘,
        ‘age‘=>23
    ];

    $smarty->assign(‘name‘, $name);
    $smarty->assign(‘array‘,$array);
    $smarty->assign(‘array2‘,$array2);

    $smarty->display(‘index.tpl‘);

index.tpl

name: {$name}<br>
array:{$array[1]}<br>
array2:{$array2.name}<br>

{*显示对象属性:{$obj->name}*}
{*对象成员方法:{$obj->show()}*}

变量配置文件内的变量:{#foo#} 或{$smarty.config.foo} <-- 等同于{#foo#}

<hr>
数学运算:<br>
{6 + 5}<br>
{6 - 5}<br>
{3 * 5}<br>
{10 / 5}<br>

<hr>
函数使用:<br>
{$strlen = strlen($name)}
{$strlen}

函数语法

index.php

<?php
//引入Smarty.class.php
require_once(‘./SmartySetup.php‘);

$smarty = new SmartySetup();
$name = ‘smarty‘;

$smarty->assign(‘name‘, $name);
$smarty->assign(‘logged_in‘, true);

$smarty->display(‘index.tpl‘);

/templates/header.tpl

header tpl<br>

/templates/footer.tpl

<br>footer tpl<br>

/templates/index.tpl

{config_load file="../config/smarty.conf"}

{include file="header.tpl"}


{if $logged_in}
    Welcome, <span style="color:{#fontColor#}">{$name}!</span>
{else}
    hi, {$name}
{/if}

{include file="footer.tpl"}

访问index.php

header tpl
Welcome, smarty!
footer tpl

内置函数

/templates/index.tpl

变量赋值

{$name=‘Bob‘}
The value of $name is {$name}.

append

数组变量增加或者创建值<br>
{append var=‘name‘ value=‘Bob‘ index=‘first‘}
{append var=‘name‘ value=‘Meyer‘ index=‘last‘}

数组:<br>
{$name.first}<br>
{$name.last}<br>

assign

模板中赋值给变量:<br>
{assign var="name" value="Bob"}

The value of $name is {$name}.

config_load

加载配置变量:<br>
{config_load file="example.conf"}

debug 在页面上显示调试控制台

{debug}

extends 继承模板

{extends file=‘parent.tpl‘}

for循环

<ul>
    {for $foo=1 to 3}
        <li>{$foo}</li>
    {/for}
</ul>

<ul>
    {for $foo=3 to 10 max=5}
        <li>{$foo}</li>
    {/for}
</ul>

foreach

{append var=‘myColors‘ value=‘red‘}
{append var=‘myColors‘ value=‘green‘}
{append var=‘myColors‘ value=‘blue‘}

<ul>
    {foreach $myColors as $color}
        <li>{$color}</li>
    {/foreach}
</ul>

{foreach $myColors as $color}
    <li>{$color@key}: {$color}</li>
{/foreach}
</ul>
{append var=‘myColors‘ value=‘red‘}
{append var=‘myColors‘ value=‘green‘}
{append var=‘myColors‘ value=‘blue‘}


<ul>
{foreach $myColors as $color}
    <li>
        {*index是当前数组索引,从0开始计算。*}
        {$color@index}
        {*iiteration是当前循环的次数*}
        {$color@iteration}
        {*first 首次循环时*}
        {$color@first}
        {*last 最后一次时, last将为TRUE*}
        {$color@last}
        {*total 总次数*}
        {$color@total}
    </li>

{/foreach}
</ul>

break

{$data = [1,2,3,4,5]}
{foreach $data as $value}
    {if $value == 3}
        {break}
    {/if}
    {$value}
{/foreach}

continue

{$data = [1,2,3,4,5]}
{foreach $data as $value}
    {if $value == 3}
        {continue}
    {/if}
    {$value}
{/foreach}

function

{function add}
    {$total = 0}
    {foreach $data as $val}
        {$total = $total + $val}
    {/foreach}
    {$total}
{/function}

{append var=‘data‘ value="2"}
{append var=‘data‘ value="3"}
{append var=‘data‘ value="4"}

{add data=$data}

{if}...{elseif}..{else}

{$name = ‘Blog‘}
{$bool = true}

{if isset($name) && $name == ‘Blog‘}
Welcome, Blog;
{elseif $name == ‘Lili‘}
Welcome,Lili
{else}
Welcome
{/if}

{if $bool}
    it is true;
{/if}

include 载入模板

{include file=‘page_header.tpl‘}

nocache 关闭模板区块的缓存

Today‘s date is
{nocache}
{$smarty.now|date_format}
{/nocache}

section 循环遍历

数组:
{append var=‘array‘ value=‘1‘}
{append var=‘array‘ value=‘2‘}
{append var=‘array‘ value=‘3‘}

{section name=index loop=$array step=1}
    {$array[index]}<br />
{/section}

数组对象:
{append var=‘obj‘ value=‘smarty‘ index=‘name‘}
{append var=‘obj‘ value=‘5‘ index=‘age‘}
{append var=‘obj‘ value=‘1527808122‘ index=‘phone‘}

{append var=‘obj2‘ value=‘smarty2‘ index=‘name‘}
{append var=‘obj2‘ value=‘8‘ index=‘age‘}
{append var=‘obj2‘ value=‘1527822222‘ index=‘phone‘}

{append var=‘array2‘ value=$obj}
{append var=‘array2‘ value=$obj2}

{section name=index loop=$array2 step=1}
    name:{$array2[index].name} | age:{$array2[index].age} | phone:{$array2[index].phone}<br />
{/section}

strip 过滤掉多余的空格和回车

strip}
<table border=‘0‘>
 <tr>
  <td>
   <a href="{$url}">
    <font color="red">This is a test</font>
   </a>
  </td>
 </tr>
</table>
{/strip}

while 循环

{append var=‘array‘ value=‘value1‘}
{append var=‘array‘ value=‘value2‘}
{append var=‘array‘ value=‘value3‘}

{$i = 0}
{while $i < count($array)}
    {$array[$i]}
    {$i = $i + 1}
{/while}

自定义函数

counter 计数器

{counter start=2 skip=2}<br />
{counter}

cycle 交替循环一系列值

<style>
    .odd{
        background-color: red;
    }
    .even{
        background-color: blue;
    }
</style>
{append var=‘data‘ value=‘data1‘}
{append var=‘data‘ value=‘data2‘}
{append var=‘data‘ value=‘data3‘}

<table>
    {section name=index loop=$data}
        <tr class="{cycle values="odd,even"}">
            <td>{$data[index]}</td>
        </tr>
    {/section}
</table>

fetch 用于获取文件内容、HTTP或者FTP内容,以便输出

{fetch file=‘http://www.baidu.com‘}

配置文件

/config/smarty.conf

pageTitle = "Main Menu"
bodyBgColor = #000000
tableBgColor = #000000
rowBgColor = #00ff00

缓存

开启缓存

<?php
//引入Smarty.class.php
require_once(‘./SmartySetup.php‘);

$smarty = new SmartySetup();
//开启缓存
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$name = ‘smarty‘;

$smarty->assign(‘name‘, $name);

$smarty->display(‘index.tpl‘);

设置缓存时间

// 设置index.tpl的过期时间为30秒
$smarty->setCacheLifetime(30);

isCached()

$name = ‘smarty‘;

if (!$smarty->isCached(‘index.tpl‘)) {
    $smarty->assign(‘name‘, $name);
}

删除缓存

// 仅删除index.tpl的缓存
$smarty->clearCache(‘index.tpl‘);

// 删除全部缓存
$smarty->clearAllCache();

PHP smarty快速入门

上一篇:手写实现js Promise


下一篇:HttpRunner2.X开源接口测试框架学习(六):参数化设置