1、下载
从官网或github上下载资源包,将资源包中的libs文件夹放置在项目根目录方便使用
2、smarty的简单使用
在项目根目录新建模板文件template.html:
<!DOCTYPE html> <html> <head> <title>hello</title> </head> <body> {$str} </body> </html>
代码解读:smarty模板引擎的默认左右定界符是左右大括号{},表示里面的内容需要smarty引擎解析,其中$str代表一个变量
在项目根目录新建php文件index.php:
<?php //引入smarty核心类 include_once 'libs/Smarty.class.php'; //实例化一个smarty对象 $s = new Smarty(); //分配数据:将'hello world'替换掉$str $s->assign('str','hello world'); //显示模板数据 $s->display('template.html');
smarty编译模板并在项目根目录的template_c目录下生成一个编译后的php文件,
最终会将页面解析成以下代码:
<!DOCTYPE html> <html> <head> <title>hello</title> </head> <body> hello world </body> </html>
3、smarty简单配置
$s = new Smarty(); $s->debugging = false; //是否开启debug调试,默认关闭 $s->template_dir = 'templates/'; //模板目录,若该目录不存在,则默认在当前目录下寻找模板文件 $s->compile_dir = 'templates_c'; //编译目录,不存在则自动创建 $s->config_dir = 'configs/'; //配置项目录 $s->caching = false; //是否开启缓存 $s->cache_dir = 'cache/'; //缓存目录 $s->cache_lifetime = 3600; //缓存生命周期(秒) $s->left_delimiter = '{'; //左定界符 $s->right_delimiter = '}'; //右定界符
4、模板变量
smarty模板中的变量分三类
- PHP分配变量,在assign方法分配的变量,assign一般会分配三种数据类型给模板使用
- 标量数据:使用$变量名访问
- 数组数据:使用$变量名.下标或$变量名[下标]访问
- 对象数据:使用$变量名->属性名访问
- 例
- PHP代码
include_once 'libs/Smarty.class.php'; $s = new Smarty(); //标量分配 $s->assign('str','hello world'); //数组分配 $s->assign('arr',array(1,2,3)); class Person{ public $name = 'testobj'; public $age = 12; } //对象分配 $s->assign('obj',new Person()); $s->display('template.html');
- 模板代码
<!DOCTYPE html> <html> <head> <title>hello</title> </head> <body> {*注释:使用标量数据*} {$str} {*注释:使用数组数据*} {$arr.0}----{$arr[1]} {*注释:使用对象数据*} {$obj->name}----{$obj->age} </body> </html>
- 页面最终结果代码
<!DOCTYPE html> <html> <head> <title>hello</title> </head> <body> hello world 1----2 testobj----12 </body> </html>
- PHP代码
- GET数据:{$smart.get.下标}
- POST数据:{$smart.post.下标}
- Session数据:{$smart.session.下标}
- Cookie数据:{$smart.cookie.下标}
- REQUEST数据:{$smart.request.下标}
- Server数据:{$smart.server.大写下标}
- 时间戳:{$smart.now}
- 模板路径:{$smart.current_dir}
- 模板名字:{$smart.template}
- 配置文件:{$smart.config.下标}
- 自定义变量:在模板中定义变量并使用
- 语法:{assign var='变量名' value='变量值'}
- 例:
<!DOCTYPE html> <html> <head> <title>hello</title> </head> <body> {*注释:使用标量数据*} {$str} {*注释:使用数组数据*} {$arr.0}----{$arr[1]} {*注释:使用对象数据*} {$obj->name}----{$obj->age} {*声明自定义变量*} {assign var='num' value='10086'} {*使用自定义变量*} {$num} </body> </html>
5、模板配置文件
配置文件格式:可以直接使用txt文件
配置项格式:配置名称 = 值
使用#代表注释,默认所有配置项都是全局的,[局部]代表局部访问
#注释:全局配置 bgColor = #00ff00 #局部配置 [customer] title = 'customer_title'
在模板文件中加载配置文件:
<!DOCTYPE html> <html> <head> {*配置文件加载*} {config_load file='smarty_config/config.txt'} {*使用$smarty.config.配置名称访问,这里无法访问局部变量*} <title>{$smarty.config.title}</title> </head> {*使用#配置名称#访问配置项*} <body bgcolor="{#bgColor#}"></body> </html>
6、模板流程控制语法结构
- 判断:
{if 判断语句1}
分支1
{elseif 判断语句2}
分支2
{else}
分支3
{/if} 结束
- 循环:
{*形式1:原始foreach模式*}
{foreach $arr as $var}
循环体
{/foreach} 结束
{*形式2:smarty专属模式*}
{foreach from=$arr key='键名' item='值名'}
循环体
{/foreach} 结束
{*使用以下方式也可以取得键名*}
{$值名@key}
smarty foreach循环可访问属性,使用$循环变量名@属性访问
属性名 | 说明 |
index | 当前数组索引(循环内部使用) |
iteration | 当前循环次数(循环内部使用) |
first | 首次循环(循环内部使用) |
last | 最后一次循环(循环内部使用) |
show | 循环是否执行判定:true表示有数据,false表示没数据(循环内外部都可以使用) |
total | foreach执行总次数(循环内外部都可以使用) |
{assign var='p' value=array('name'=>'hzc','age'=>20,'sex'=>1)} {if is_array($p)} {foreach $p as $v1} {$v1@key}=>{$v1} {/foreach} {foreach $p as $k2=>$v2} {$k2}=>{$v2} {/foreach} {foreach from=$p key='k3' item='v3'} 第{$v3@iteration}次循环:{$v3@index}:{$v3@key}=>{$v3} {/foreach} {if $v3@show}一共循环了{$v3@total}次{/if} {/if}
7、文件包含
{include file='filename'}
index.html
<!DOCTYPE html> <html> <head></head> <body> {include file='arr.tpl'} </body> </html>
arr.tpl
{assign var='p' value=array('name'=>'hzc','age'=>20,'sex'=>1)} {if is_array($p)} {foreach from=$p key='k' item='v'} 第{$v@iteration}次循环:{$k}=>{$v} {/foreach} {/if}
8、前端语法保护
smarty不会解析{literal}和{/literal}之间的代码,可以防止误解析
9、使用外部函数
smarty支持在定界符内直接使用系统函数和用户自定义函数,只要保证函数在内存中即可