文件下載
1、下載地址:http://www.smarty.net/
2、我下載的版本是3.1.27 ,將下載的文件smarty-3.1.27.zip解壓出來,然後將libs文件夾的所有文件複製到你的項目中(可重新命名libs文件夾名稱)
3、目錄結構,如下圖
4、本文相關實例完整代碼下載:下載地址
Smarty相關配置
1、配置代碼
//獲取硬路徑
define('ROOT_PATH', dirname(__FILE__).'/');
//引入smarty核心類文件
require ROOT_PATH.'smarty/Smarty.class.php';
//自定義模板文件目錄,此時是將Smarty默認路徑重寫了一遍
$_smarty->template_dir = './templates/';
//編譯文件目錄
$_smarty->compile_dir = './templates_c/';
//配置變量目錄
$_smarty->config_dir = './configs/';
//緩存目錄
$_smarty->cache_dir = './cache/';
//是否開啓緩存,開發階段,要關閉緩存,1/true表示開啓緩存
$_smarty->caching = 1;
//緩存的時間,默認是1小時
$_smarty->cache_lifetime = 60*60*24;
//左定界符,{}是Smarty默認的左右定界符
$_smarty->left_delimiter = '{';
//右定界符
$_smarty->right_delimiter = '}';
2、配置說明
(1)在項目下面要提前新建幾個文件夾:templates(放模板文件,如:index.tpl)、templates_c(放編譯文件,如、debef1afdc8f3ca548e8845c5d6c8dc1a075b2d5_0.file.index.tpl.php)、configs(放配置文件,如:config.conf)、cache(放緩存文件,如:debef1afdc8f3ca548e8845c5d6c8dc1a075b2d5.index.tpl.php)。其中templates_c和cache文件夾會自動生成,其相應的編譯文件和緩存文件也會自動生成。config目錄下的配置文件的後綴可以隨便定義,爲了容易識別一般定義爲.conf
(2)左右定界符定義成{ }後,.tpl文件裏面就不能像html文件裏面那樣寫JS和CSS代碼(符號衝突)。解決辦法:可以將定界符定義成其他字符或字符串,還可以將{ }用Smarty的內置函數打印出來,不過常用的方法是將JS、CSS代碼和HTML代碼(.tpl文件中)分離。
(3).tpl文件類似於HTML文件,不同的是按照smarty語法可以解析裏面的一些變量。.conf配置文件的書寫格式是:鍵=值 (每一行表示一個鍵值對,鍵、值都不需要引號)
Smarty解析變量
實例代碼:
smarty.inc.php(下面的實例也會用到)
<?php
//獲取硬路徑
define('ROOT_PATH', dirname(__FILE__).'/');
//引入smarty核心類文件
require ROOT_PATH.'smarty/Smarty.class.php';
//實例化Smarty對象
$_smarty = new Smarty();
//自定義模板文件目錄,此時是將Smarty默認路徑重寫了一遍
$_smarty->template_dir = './templates/';
//編譯文件目錄
$_smarty->compile_dir = './templates_c/';
//配置變量目錄
$_smarty->config_dir = './configs/';
//緩存目錄
$_smarty->cache_dir = './cache/';
//是否開啓緩存,開發階段,要關閉緩存,1/true表示開啓緩存
$_smarty->caching = 1;
//緩存的時間,默認是1小時
$_smarty->cache_lifetime = 60*60*24;
//左定界符,{}是Smarty默認的左右定界符
$_smarty->left_delimiter = '{';
//右定界符
$_smarty->right_delimiter = '}';
?>
config.conf(下面的實例也會用到)
site=小易blog
url=http://blog.csdn.net/oldinaction
index.php
<?php
//導入smarty配置文件
require 'smarty.inc.php';
$_title = 'Smarty模板引擎';
$_array1 = array('1', '2', '3');
$_array2 = array('1'=>'一', '2'=>'二', '3'=>'三');
Class Smalle {
public $site = 'AEZO';
public function url(){
return 'http://www.aezo.cn';
}
}
define('SMALLE', '微信號:smallelife');
//分配普通變量,第一個參數是Smarty模板引擎變量,第二個參數是PHP變量
$_smarty->assign('title', $_title);
$_smarty->assign('a', 1);
$_smarty->assign('b', 2);
//分配數組變量
$_smarty->assign('array1', $_array1);
$_smarty->assign('array2', $_array2);
//分配對象變量
$_smarty->assign('smalle', new Smalle());
//引入模板
$_smarty->display('index.tpl');
?>
index.tpl
{config_load file='config.conf'} {*註釋:此時文件目錄不需config這一層,因smarty.inc.php已經定義了配置文件目錄*}
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{$title}</title>
</head>
<body>
數組變量顯示結果:<br />
{$array1[1]}<br />
{$array2.1}<br />
----------------------<br />
對象變量顯示結果:<br />
{$smalle->site}<br />
{$smalle->url()}<br />
----------------------<br />
模板中可進行計算:<br />
{$a+$b}<br />
----------------------<br />
Smarty保留變量:<br />
{*註釋:保留的$smarty.const變量專門針對常量,這樣就不需要將常量像變量一樣分配了*}
{$smarty.const.SMALLE}<br />
----------------------<br />
config目錄下的配置文件中的鍵值對:<br />
{*註釋:下面兩種表示方法都可以*}
{$smarty.config.site}<br />
{#url#}<br />
----------------------<br />
</body>
</html>
效果展示:
在瀏覽器訪問 http://127.0.0.1/smarty/index.php 效果如下
Smarty自定義函數
此處說的自定義函數主要包括Smarty提供的自定義函數和開發者自己寫的函數稱爲自定義函數。開發者以插件的形式自定義的函數,文件要放在Smarty核心文件的plugins目錄下。
function.smfunc.php 自定義函數文件,放在plugins目錄下
<?php
/**
* Smalle 以插件的形式自定義的函數
* 注意:
* 1、文件格式爲function.xxx.php
* 2、函數名爲smarty_function_xxx($params){}
* 3、使用:{smfunc a=1 b=2} a、b參數和其值會以關聯數組的形式傳給此函數的第一個參數
*/
function smarty_function_smfunc($_arr){
$_cout = $_arr['a'] + $_arr['b'];
return $_cout;
}
?>
func.php<?php
//導入smarty配置文件
require 'smarty.inc.php';
$_smarty->assign('array', array('a'=>'一','b'=>'二','c'=>'三'));
//引入模板
$_smarty->display('func.tpl');
?>
func.tpl<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>自定義函數</title>
</head>
<body>
Smarty的自定義函數舉例:<br />
{html_checkboxes options=$array selected=b}<br />
----------------------<br />
開發者以插件的形式自定義的函數舉例:<br />
{smfunc a=1 b=2}<br />
----------------------<br />
</body>
</html>
效果展示:
Smarty內置函數
builtFunc.php
<?php
//導入smarty配置文件
require 'smarty.inc.php';
/**
* {insert}內置函數中調用的即爲此處的函數
* 注意:
* 1、函數命名:insert_xxx (xxx指{insert}內置函數中name的值)
* 2、{insert}內置函數中的參數會以數組的形式傳遞給此函數
*/
function insert_getConfig($_arr){
return $_arr['site'].':'.$_arr['url'];
}
$_smarty->assign('arr',array('a'=>'一','b'=>'二','c'=>'三'));
//引入模板
$_smarty->display('builtFunc.tpl');
?>
builtFunc.tpl
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>內置函數</title>
</head>
<body>
內置函數舉例1:<br />
----------------------示例1<br />
{assign var="flag" value=false}
{assign var="i" value=1}
{if $flag}
0
{elseif $i == 2}
2
{else $i < 2}
1
{/if}
<br /><br />
----------------------示例2<br />
{foreach $arr as $item}
{$item@key}=>{$item}
{/foreach}<br />
一共循環了{$item@total}次<br />
{foreach $arr as $key=>$value}
{$key}=>{$value}
{/foreach}<br /><br />
----------------------示例3<br />
{for $var=1 to 10}
{$var}
{/for}<br />
{for $var=2 to 10 step 2}
{$var}
{/for}<br />
{for $var=2 to 10 max=3}
{$var}
{/for}<br /><br /><br />
內置函數舉例2:<br />
----------------------示例4(顯示左右定界符)<br />
{ldelim} {rdelim}<br /><br />
----------------------示例5(literal顯示代碼塊)<br />
{literal}
function(){
alert(1);
}
{/literal}<br /><br />
----------------------示例6<br />
{assign var="name" value="smalle"}
{$name}<br /><br />
-->{ldelim}config_load file="example.conf"{rdelim}用來從配置文件中加載config變量到模版<br />
使用 $smarty.config.鍵 或者 #鍵# 獲取<br /><br />
-->{ldelim}strip{rdelim}{ldelim}/strip{rdelim}壓縮源代碼,去掉空格和換行
----------------------示例7<br />
{strip}
<table border=0>
<tr>
<td>
<font color="red">This is a test</font>
</td>
</tr>
</table>
{/strip}<br />
-->{ldelim}insert name="insertName"{rdelim}插入函數。所包含的內容不會被緩存,每次調用模板都會執行<br />
----------------------示例8<br />
{config_load file="config.conf"}
{insert name="getConfig" site=#site# url=#url#}<br />
</body>
</html>
效果展示:
Smarty變量調節器
同自定義函數一樣,開發者也可以以插件的形式自定義變量調節器,當然Smarty也提供了一些變量調節器。自定義的插件文件也要放在plugins目錄下。
modifier_smtruncate.php(放在plugins目錄下)
<?php
/**
* Smalle 自定義變量調節器(截取字符串)
* 注意:
* 1、文件命名格式:modifier.xxx.php
* 2、函數的命名格式:smarty_modifier_xxx($params){}
* 3、使用:{$str|smtruncate:0:20:'utf-8':'###'} $str是注入進來的變量,會傳給函數的第一個參數
*/
function smarty_modifier_smtruncate($_str, $_length, $_start=0, $_encoding='utf-8', $_endStr='...'){
if(mb_strlen($_str) > $_length){
return mb_substr($_str, $_start, $_length, $_encoding);
} else {
return mb_substr($_str, $_start, $_length, $_encoding).$_endStr;
}
}
?>
mvar.php
<?php
//導入smarty配置文件
require 'smarty.inc.php';
$_url = 'http://www.aezo.cn/tag=<strong>小易你好啊</strong>';
$_smarty->assign('url', $_url);
//引入模板
$_smarty->display('mvar.tpl');
?>
mvar.tpl
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>變量調節器</title>
</head>
<body>
Smarty的變量調節器舉例<br />
--regex_replace[正則替換]<br />
{$url|regex_replace:"/http/":"https"}<br /><br />
--strip_tags[去除html標籤],參數false指標籤不用空格替換<br />
{$url|strip_tags:false}<br /><br />
--組合變量調節器<br />
{$url|regex_replace:"/http/":"https"|strip_tags:false}<br />
----------------------<br /><br />
開發者以插件的形式自定義的變量調節器舉例:<br />
{$url|smtruncate:33}<br />
----------------------<br />
</body>
</html>
效果展示:
Smarty緩存機制
要先開啓緩存($_smarty->caching = 1;),下面的功能才能實現
cache.php
<?php
//設置時區
date_default_timezone_set('Asia/Shanghai');
//導入smarty配置文件
require 'smarty.inc.php';
//多重緩存,訪問 http://127.0.0.1/smarty/cache.php?id=1&page=1 改變參數的值
$_id = @$_GET['id'];
//部分片段不緩存
function smarty_block_nocache($param, $content, $smarty) {
return $content;
}
//註冊插件
$_smarty->registerPlugin('block','nocache', 'smarty_block_nocache', false);
//清除所有緩存
//$_smarty->clearAllCache();
//引入模板,多重緩存時要加上第二個參數(可以有多個參數)
//$_smarty->display('cache.tpl', $_id);//多重緩存時也可以將URL中的參數全部傳給後面的參數
$_smarty->display('cache.tpl', $_SERVER["REQUEST_URI"]);
?>
cache.tpl
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>緩存機制</title>
</head>
<body>
Smarty的的多重緩時存獲取參數:<br />
{$id}
<br />
----------------------<br />
開啓緩存後,讓部分代碼不緩存(利用自定義函數):<br />
{'0'|date_format:'%Y-%m-%d %H:%M:%S'}<br />
{nocache}
{'0'|date_format:'%Y-%m-%d %H:%M:%S'}
{/nocache}<br />
----------------------<br />
</body>
</html>
效果展示:
如果訪問http://127.0.0.1/smarty/cache.php?id=1&page=1,則會自動生成如_smarty_cache_php_id_1_page_1^de087441c95bd03a91d833c6b344eb35bda77af9.cache.tpl.php的緩存文件。
相信大家掌握了這些,基本就可以在項目中使用Smarty了,更深入的研究就留給你們自己吧!
*************************************************************
PHP開發交流QQ羣:127255239 驗證消息:smcsdn
羣成員有限,先到先得會員資格,請認真填寫驗證消息
資源都在這裏,你還在到處閒逛什麼呢?
大神都在這裏,你還在到處閒逛什麼呢?
*************************************************************