第一個(gè)smarty程序
smarty配置
//定義服務(wù)器的絕對(duì)路徑 通過(guò)全局變量
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);
//定義smarty目錄的絕對(duì)路徑
define('SMARTY_PATH','\smarty\\');
//加載smarty類庫(kù)文件
require BASE_PATH.SMARTY_PATH.'Smarty.class.php';
//實(shí)例化一個(gè)smarty對(duì)象
$smarty=new smarty();
//定義各個(gè)目錄的路徑
$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/'; //定義模板目錄存儲(chǔ)位置
$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/'; //定義編譯目錄存儲(chǔ)位置
$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/'; //定義配置文件存儲(chǔ)位置
$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/'; //定義模板緩存目錄
//使用smarty賦值方法將一對(duì)名稱/方法發(fā)送到模板中
//assign用于在模板被執(zhí)行時(shí)為模板變量賦值
$smarty->assign('title','第一個(gè)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個(gè)目錄:templates, templates_c, configs, cache
2.創(chuàng)建配置文件configs.php,使用時(shí)只要include配置文件即可:
//定義服務(wù)器的絕對(duì)路徑 通過(guò)全局變量
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);
//定義smarty目錄的絕對(duì)路徑
define('SMARTY_PATH','\smarty\\');
//加載smarty類庫(kù)文件
require BASE_PATH.SMARTY_PATH.'Smarty.class.php';
//實(shí)例化一個(gè)smarty對(duì)象
$smarty=new smarty();
//定義各個(gè)目錄的路徑
$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/'; //定義模板目錄存儲(chǔ)位置
$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/'; //定義編譯目錄存儲(chǔ)位置
$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/'; //定義配置文件存儲(chǔ)位置
$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/'; //定義模板緩存目錄
注釋
smarty中的注釋包含在兩個(gè)星號(hào)中,格式如{*注釋*}
變量
1.php頁(yè)面中的變量
即assign方法傳過(guò)來(lái)的變量,需使用$符號(hào)。
對(duì)于數(shù)組如$arr=array{'object'->'book','type'->'computer','unit'->'本'},取type值有兩種方法,一種用索引,一種通過(guò)鍵值如$arr.type。
2.保留變量
相當(dāng)于php中的預(yù)定義變量,模板中使用保留變量時(shí)無(wú)須使用assign方法傳值而只需使用變量名。
常用保留變量:
- get,post,server,session,cookie,request - 等價(jià)于php的$_GET等
- now - 等價(jià)于php的time
- const - 用const包含修飾的為常量
- config - 配置文件內(nèi)容變量
<body>
變量type的值是:{$smarty.get.type}<br>
當(dāng)前路徑:{$smarty.server.PHP_SELF}<br>
當(dāng)前時(shí)間為:{$smarty.now}
</body>
3.從配置文件中讀取數(shù)據(jù)
- 將變量名置于兩個(gè)
#中間 - 使用保留變量中的
$smarty_config來(lái)調(diào)用配置文件
{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='調(diào)用配置文件'
bgcolor='#f0f0f0'
border='5'
type='計(jì)算機(jī)類'
name='php從入門(mén)到精通'
修飾變量 - 對(duì)變量進(jìn)行處理
格式:
{varible_name(變量名)|modifer_name(修飾變量的方法名):parameter1:...(參數(shù)值,多個(gè)參數(shù)用:分隔開(kāi))}
index.php文件內(nèi)容:
<?php
//載入配置文件
include './smarty/configs/config.php';
$str1='這是一個(gè)實(shí)例';
$str2="\n圖書(shū)->計(jì)算機(jī)類->php\n書(shū)名:《從入門(mén)到精通》";
$str3="\n價(jià)格:¥30/本。"; // 轉(zhuǎn)義字符的時(shí)候雙引號(hào)會(huì)替換變量的值,而單引號(hào)會(huì)把它當(dāng)做字符串輸出。帶有\(zhòng)n等需轉(zhuǎn)義的字符串一定要是雙引號(hào)。
$smarty->assign('title','使用變量修飾方法');
$smarty->assign('str',$str1.$str2.$str3);
//要顯示的模板頁(yè)面
$smarty->display('04/index.html');
?>
index.html文件內(nèi)容:
<!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:變量中的字符串格式,如果有參數(shù)true則空格也被計(jì)算 -->
變量中的字符數(shù)(包括空格):{$str|count_characters:true}<br>
<!-- nl2br:所有的換行符被替換成<br> -->
使用變量修飾方法后:{$str|nl2br}
</body>
</html>
輸出:
原文:這是一個(gè)實(shí)例 圖書(shū)->計(jì)算機(jī)類->php 書(shū)名:《從入門(mén)到精通》 價(jià)格:¥30/本。
變量中的字符數(shù)(包括空格):42
使用變量修飾方法后:這是一個(gè)實(shí)例
圖書(shū)->計(jì)算機(jī)類->php
書(shū)名:《從入門(mén)到精通》
價(jià)格:¥30/本。
流程控制
1.if...elseif...else
{if 條件語(yǔ)句1}
語(yǔ)句1
{elseif 條件語(yǔ)句2}
語(yǔ)句2
{/if}
- 必須以
/if為結(jié)束標(biāo)志 - 除了常見(jiàn)運(yùn)算符外還可以使用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文件內(nèi)容:
<?php
//載入配置文件
include './smarty/configs/config.php';
$smarty->assign('title','if 條件判斷語(yǔ)句');
//要顯示的模板頁(yè)面
$smarty->display('05/index.html');
?>
index.html文件內(nèi)容:
<!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'}
康薩思密達(dá)~,{$smarty.get.type}
{else}
撒油拉拉~
{/if}
</body>
</html>
如果url參數(shù)type=kong,輸出:
康薩思密達(dá)~,kong
如果不是,輸出:
撒油拉拉~
2.foreach循環(huán)控制 - 可以循環(huán)輸出數(shù)組
格式:
{foreach name=foreach_name key=key item=item from=arr_name}
...
{/foreach}
name - 該循環(huán)名稱,key - 當(dāng)前元素鍵值,item - 當(dāng)前元素變量名,from - 該循環(huán)的數(shù)組 ,item 和from不可省略
index.php文件內(nèi)容:
<!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循環(huán)輸出數(shù)組。<br>
{foreach key=key item=item from=$infobook}
{$key}=>{$item}<br>
{/foreach}
</body>
</html>
index.html文件內(nèi)容:
<?php
//載入配置文件
include './smarty/configs/config.php';
$infobook=array('object'=>'book','type'=>'computer','name'=>'從精通到入門(mén)','publishing'=>'清華出版');
$smarty->assign('title','使用foreach循環(huán)輸出數(shù)組');
$smarty->assign('infobook',$infobook);
//要顯示的模板頁(yè)面
$smarty->display('06/index.html');
?>
輸出:
使用foreach循環(huán)輸出數(shù)組:
object=>book
type=>computer
name=>從精通到入門(mén)
publishing=>清華出版
3.section循環(huán)控制 - 可以用于比較復(fù)雜的語(yǔ)句
格式:
{section name="sec_name" loop=$arr_name start=num step=num}
name-該循環(huán)的名稱,如果是數(shù)組則為數(shù)組索引下標(biāo), loop- 循環(huán)的數(shù)組, start- 循環(huán)的初始位置, step- 步長(zhǎng),如step=2表示循環(huán)一次數(shù)組后數(shù)組指針將向下移動(dòng)兩位。
index.php文件內(nèi)容:
<?php
//載入配置文件
include './smarty/configs/config.php';
$obj=array(
array(
'id'=>1,
'bigclass'=>'計(jì)算機(jī)圖書(shū)',
'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'=>'語(yǔ)文'
))),
array(
'id'=>3,
'bigclass'=>'暢銷小說(shuō)',
'smallclass'=>array(
array(
's_id'=>4,
's_type'=>'網(wǎng)絡(luò)小說(shuō)'
),
array(
's_id'=>5,
's_type'=>'科幻小說(shuō)'
)))
);
$smarty->assign('title','section循環(huán)控制');
$smarty->assign('obj',$obj);
//要顯示的模板頁(yè)面
$smarty->display('07/index.html');
?>
index.html文件內(nèi)容:
<!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>
輸出:
計(jì)算機(jī)圖書(shū)
php
歷史傳記
歷史
語(yǔ)文
暢銷小說(shuō)
網(wǎng)絡(luò)小說(shuō)
科幻小說(shuō)
smarty中的常用方法
以append()為例
index.php文件內(nèi)容:
<?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);
//要顯示的模板頁(yè)面
$smarty->display('08/index.html');
?>
index.html文件內(nèi)容:
<!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()語(yǔ)法:
append(string varname, mixed var,bool merge)
第三個(gè)參數(shù)為true:該值將與當(dāng)前數(shù)組合并
第三個(gè)參數(shù)為false:該值將與當(dāng)前數(shù)組附加