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
//引入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使用
//引入Smarty.class.php
require_once(‘./SmartySetup.php‘);
$smarty = new SmartySetup();
$name = ‘smarty‘;
$smarty->assign(‘name‘,$name);
$smarty->display(‘index.tpl‘);
基本语法
注释
{* 这是一个注释 *}
变量
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}
array:{$array[1]}
array2:{$array2.name}
{*显示对象属性:{$obj->name}*}
{*对象成员方法:{$obj->show()}*}
变量配置文件内的变量:{#foo#} 或{$smarty.config.foo} <-- 等同于{#foo#}
数学运算:
{6 + 5}
{6 - 5}
{3 * 5}
{10 / 5}
函数使用:
{$strlen = strlen($name)}
{$strlen}
函数语法
index.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
/templates/footer.tpl
<br>footer tpl<br>
/templates/index.tpl
{config_load file="../config/smarty.conf"}
{include file="header.tpl"}
{if $logged_in}
Welcome, "color:{#fontColor#}">{$name}!
{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
数组变量增加或者创建值
{append var=‘name‘ value=‘Bob‘ index=‘first‘}
{append var=‘name‘ value=‘Meyer‘ index=‘last‘}
数组:
{$name.first}
{$name.last}
assign
模板中赋值给变量:
{assign var="name" value="Bob"}
The value of $name is {$name}.
config_load
加载配置变量:
{config_load file="example.conf"}
debug 在页面上显示调试控制台
{debug}
extends 继承模板
{extends file=‘parent.tpl‘}
for循环
{for $foo=1 to 3}
- {$foo}
{/for}
{for $foo=3 to 10 max=5}
- {$foo}
{/for}
foreach
{append var=‘myColors‘ value=‘red‘}
{append var=‘myColors‘ value=‘green‘}
{append var=‘myColors‘ value=‘blue‘}
{foreach $myColors as $color}
- {$color}
{/foreach}
{foreach $myColors as $color}
{$color@key}: {$color}
{/foreach}
{append var=‘myColors‘ value=‘red‘}
{append var=‘myColors‘ value=‘green‘}
{append var=‘myColors‘ value=‘blue‘}
{foreach $myColors as $color}
-
{*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]}
{/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}
{/section}
strip 过滤掉多余的空格和回车
strip}
<table border=‘0‘>
<tr>
<td>
<a href="{$url}">
<font color="red">This is a testfont>
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}
{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
缓存
开启缓存
//引入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();
原文:https://www.cnblogs.com/hu308830232/p/14959916.html