第一个smarty程序
smarty配置
//定义服务器的绝对路径 通过全局变量
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);
//定义smarty目录的绝对路径
define('SMARTY_PATH','\smarty\\');
//加载smarty类库文件
require BASE_PATH.SMARTY_PATH.'Smarty.class.php';
//实例化一个smarty对象
$smarty=new smarty();
//定义各个目录的路径
$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/'; //定义模板目录存储位置
$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/'; //定义编译目录存储位置
$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/'; //定义配置文件存储位置
$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/'; //定义模板缓存目录
//使用smarty赋值方法将一对名称/方法发送到模板中
//assign用于在模板被执行时为模板变量赋值
$smarty->assign('title','第一个smarty程序');
$smarty->assign('content','hello world');
//display用于显示模板
$smarty->display('index.html');
templates目录下的index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>
smarty配置
1.新建4个目录:templates
, templates_c
, configs
, cache
2.创建配置文件configs.php
,使用时只要include配置文件即可:
//定义服务器的绝对路径 通过全局变量
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);
//定义smarty目录的绝对路径
define('SMARTY_PATH','\smarty\\');
//加载smarty类库文件
require BASE_PATH.SMARTY_PATH.'Smarty.class.php';
//实例化一个smarty对象
$smarty=new smarty();
//定义各个目录的路径
$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/'; //定义模板目录存储位置
$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/'; //定义编译目录存储位置
$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/'; //定义配置文件存储位置
$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/'; //定义模板缓存目录
注释
smarty中的注释包含在两个星号中,格式如{*注释*}
变量
1.php页面中的变量
即assign
方法传过来的变量,需使用$
符号。
对于数组如$arr=array{'object'->'book','type'->'computer','unit'->'本'}
,取type
值有两种方法,一种用索引,一种通过键值如$arr.type
。
2.保留变量
相当于php中的预定义变量,模板中使用保留变量时无须使用assign
方法传值而只需使用变量名。
常用保留变量:
- get,post,server,session,cookie,request - 等价于php的$_GET等
- now - 等价于php的time
- const - 用const包含修饰的为常量
- config - 配置文件内容变量
<body>
变量type的值是:{$smarty.get.type}<br>
当前路径:{$smarty.server.PHP_SELF}<br>
当前时间为:{$smarty.now}
</body>
3.从配置文件中读取数据
- 将变量名置于两个
#
中间 - 使用保留变量中的
$smarty_config
来调用配置文件
{config_load file="03/03.conf"}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{#title#}</title>
</head>
<body background-color="{#bgcolor#}">
<table border="{#border#}">
<tr>
<td>{$smarty.config.type}</td>
<td>{$smarty.config.name}</td>
</tr>
</table>
</body>
</html>
配置文件:
title='调用配置文件'
bgcolor='#f0f0f0'
border='5'
type='计算机类'
name='php从入门到精通'
修饰变量 - 对变量进行处理
格式:
{varible_name(变量名)|modifer_name(修饰变量的方法名):parameter1:...(参数值,多个参数用:分隔开)}
index.php文件内容:
<?php
//载入配置文件
include './smarty/configs/config.php';
$str1='这是一个实例';
$str2="\n图书->计算机类->php\n书名:《从入门到精通》";
$str3="\n价格:¥30/本。"; // 转义字符的时候双引号会替换变量的值,而单引号会把它当做字符串输出。带有\n等需转义的字符串一定要是双引号。
$smarty->assign('title','使用变量修饰方法');
$smarty->assign('str',$str1.$str2.$str3);
//要显示的模板页面
$smarty->display('04/index.html');
?>
index.html文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
原文:{$str}<br>
<!-- count_characters:true/false:变量中的字符串格式,如果有参数true则空格也被计算 -->
变量中的字符数(包括空格):{$str|count_characters:true}<br>
<!-- nl2br:所有的换行符被替换成<br> -->
使用变量修饰方法后:{$str|nl2br}
</body>
</html>
输出:
原文:这是一个实例 图书->计算机类->php 书名:《从入门到精通》 价格:¥30/本。
变量中的字符数(包括空格):42
使用变量修饰方法后:这是一个实例
图书->计算机类->php
书名:《从入门到精通》
价格:¥30/本。
流程控制
1.if...elseif...else
{if 条件语句1}
语句1
{elseif 条件语句2}
语句2
{/if}
- 必须以
/if
为结束标志 - 除了常见运算符外还可以使用eq、 ne、 neq 、gt、 lt、 lte、 le、 gte、 is even 、is odd、 is not even、 is not odd、 not、 mod、 div by、 even by、 odd by等修饰
index.php文件内容:
<?php
//载入配置文件
include './smarty/configs/config.php';
$smarty->assign('title','if 条件判断语句');
//要显示的模板页面
$smarty->display('05/index.html');
?>
index.html文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
{if $smarty.get.type=='kong'}
康萨思密达~,{$smarty.get.type}
{else}
撒油拉拉~
{/if}
</body>
</html>
如果url参数type=kong,输出:
康萨思密达~,kong
如果不是,输出:
撒油拉拉~
2.foreach循环控制 - 可以循环输出数组
格式:
{foreach name=foreach_name key=key item=item from=arr_name}
...
{/foreach}
name
- 该循环名称,key
- 当前元素键值,item
- 当前元素变量名,from
- 该循环的数组 ,item
和from
不可省略
index.php文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
使用foreach循环输出数组。<br>
{foreach key=key item=item from=$infobook}
{$key}=>{$item}<br>
{/foreach}
</body>
</html>
index.html文件内容:
<?php
//载入配置文件
include './smarty/configs/config.php';
$infobook=array('object'=>'book','type'=>'computer','name'=>'从精通到入门','publishing'=>'清华出版');
$smarty->assign('title','使用foreach循环输出数组');
$smarty->assign('infobook',$infobook);
//要显示的模板页面
$smarty->display('06/index.html');
?>
输出:
使用foreach循环输出数组:
object=>book
type=>computer
name=>从精通到入门
publishing=>清华出版
3.section循环控制 - 可以用于比较复杂的语句
格式:
{section name="sec_name" loop=$arr_name start=num step=num}
name
-该循环的名称,如果是数组则为数组索引下标, loop
- 循环的数组, start
- 循环的初始位置, step
- 步长,如step=2
表示循环一次数组后数组指针将向下移动两位。
index.php文件内容:
<?php
//载入配置文件
include './smarty/configs/config.php';
$obj=array(
array(
'id'=>1,
'bigclass'=>'计算机图书',
'smallclass'=>array(
array(
's_id'=>1,
's_type'=>'php'
))),
array(
'id'=>2,
'bigclass'=>'历史传记',
'smallclass'=>array(
array(
's_id'=>2,
's_type'=>'历史'
),
array(
's_id'=>3,
's_type'=>'语文'
))),
array(
'id'=>3,
'bigclass'=>'畅销小说',
'smallclass'=>array(
array(
's_id'=>4,
's_type'=>'网络小说'
),
array(
's_id'=>5,
's_type'=>'科幻小说'
)))
);
$smarty->assign('title','section循环控制');
$smarty->assign('obj',$obj);
//要显示的模板页面
$smarty->display('07/index.html');
?>
index.html文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
<table style="width:100;border:0; text-align:left;">
{section name=sec1 loop=$obj}
<tr>
<td colspan="2">
{$obj[sec1].bigclass}
</td>
</tr>
{section name=sec2 loop=$obj[sec1].smallclass}
<tr>
<td style="width:25"> </td>
<td>
{$obj[sec1].smallclass[sec2].s_type}
</td>
</tr>
{/section}
{/section}
</table>
</body>
</html>
输出:
计算机图书
php
历史传记
历史
语文
畅销小说
网络小说
科幻小说
smarty中的常用方法
以append()
为例
index.php文件内容:
<?php
//载入配置文件
include './smarty/configs/config.php';
$arr=array('object'=>'book','type'=>'computer');
$str1=array('name'=>'php');
$str2=array('publishing'=>'qinghua');
$smarty->assign('title','使用append');
$smarty->assign('arr',$arr);
$smarty->append('arr',$str1,true);
$smarty->append('arr',$str2);
//要显示的模板页面
$smarty->display('08/index.html');
?>
index.html文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
{foreach key=key item=item from=$arr}
{$key}=>{$item}<br>
{/foreach}
</body>
</html>
输出:
object=>book
type=>computer
name=>php
0=>Array
append()
语法:
append(string varname, mixed var,bool merge)
第三个参数为true:该值将与当前数组合并
第三个参数为false:该值将与当前数组附加