Smarty模板快速入門(mén)
更新時(shí)間:2007年01月04日 00:00:00 作者:
在PHP的世界里已經(jīng)出現(xiàn)了各式各樣的模板類(lèi),但就功能和速度來(lái)說(shuō)Smarty還是一直處于領(lǐng)先地位,因?yàn)镾marty的功能相對(duì)強(qiáng)大,所以使用起來(lái)比其他一些模板類(lèi)稍顯復(fù)雜了一點(diǎn)。現(xiàn)在就用30分鐘讓您快速入門(mén)。
一. 安裝
首先打開(kāi)網(wǎng)頁(yè)http://smarty.php.net/download.php,下載最新版本的Smarty。解壓下載的文件(目錄結(jié)構(gòu)還蠻復(fù)雜的)。接下來(lái)我演示給大家一個(gè)安裝實(shí)例,看過(guò)應(yīng)該會(huì)舉一反三的。
(1) 我在根目錄下建立了新的目錄learn/,再在learn/里建立一個(gè)目錄smarty/。將剛才解壓縮出來(lái)的目錄的libs/拷貝到smarty/里,再在smarty/里新建templates目錄,templates里新建cache/,templates/,templates_c/, config/.
(2) 新建一個(gè)模板文件:index.tpl,將此文件放在learn/smarty/templates/templates目錄下,代碼如下:
新建index.php,將此文件放在learn/下:
(3) 執(zhí)行index.php就能看到Hello World!了。
二. 賦值
在模板文件中需要替換的值用大括號(hào){}括起來(lái),值的前面還要加$號(hào)。例如{$hello}。這里可以是數(shù)組,比如{$hello.item1},{$hello.item2}…
而PHP源文件中只需要一個(gè)簡(jiǎn)單的函數(shù)assign(var , value)。
簡(jiǎn)單的例子:
*.tpl:
Hello,{$exp.name}! Good {$exp.time}
*.php:
$hello[name] = “Mr. Green”;
$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);
output:
Hello,Mr.Green! Good morning
三. 引用
網(wǎng)站中的網(wǎng)頁(yè)一般header和footer是可以共用的,所以只要在每個(gè)tpl中引用它們就可以了。
示例:*.tpl:
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}
四. 判斷
模板文件中可以使用if else等判斷語(yǔ)句,即可以將一些邏輯程序放在模板里。"eq", "ne", "neq", "gt", "lt", "lte", "le", "gte" "ge", "is even", "is odd", "is not even", "is not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "<","<=",">="這些是if中可以用到的比較??纯淳湍苤朗裁匆馑及?。
示例:
{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
五. 循環(huán)
在Smarty里使用循環(huán)遍歷數(shù)組的方法是section,如何賦值遍歷都是在模板中解決,php源文件中只要一個(gè)assign就能解決問(wèn)題。
示例:
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
六. 常見(jiàn)問(wèn)題
Smarty將所有大括號(hào){}里的東西都視為自己的邏輯程序,于是我們?cè)诰W(wǎng)頁(yè)中想插入javascript函數(shù)就需要literal的幫忙了,literal的功能就是忽略大括號(hào){}。
示例:
{literal}
<script language=javascript>
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
</script>
{/literal}
一. 安裝
首先打開(kāi)網(wǎng)頁(yè)http://smarty.php.net/download.php,下載最新版本的Smarty。解壓下載的文件(目錄結(jié)構(gòu)還蠻復(fù)雜的)。接下來(lái)我演示給大家一個(gè)安裝實(shí)例,看過(guò)應(yīng)該會(huì)舉一反三的。
(1) 我在根目錄下建立了新的目錄learn/,再在learn/里建立一個(gè)目錄smarty/。將剛才解壓縮出來(lái)的目錄的libs/拷貝到smarty/里,再在smarty/里新建templates目錄,templates里新建cache/,templates/,templates_c/, config/.
(2) 新建一個(gè)模板文件:index.tpl,將此文件放在learn/smarty/templates/templates目錄下,代碼如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Smarty</title>
</head>
<body>
{$hello}
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Smarty</title>
</head>
<body>
{$hello}
</body>
</html>
新建index.php,將此文件放在learn/下:
復(fù)制代碼 代碼如下:
<?php
//引用類(lèi)文件
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
//設(shè)置各個(gè)目錄的路徑,這里是安裝的重點(diǎn)
$smarty->template_dir = "smarty/templates/templates";
$smarty->compile_dir = "smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir = "smarty/templates/cache";
//smarty模板有高速緩存的功能,如果這里是true的話即打開(kāi)caching,但是會(huì)造成網(wǎng)頁(yè)不立即更新的問(wèn)題,當(dāng)然也可以通過(guò)其他的辦法解決
$smarty->caching = false;
$hello = "Hello World!";
//賦值
$smarty->assign("hello",$hello);
//引用模板文件
$smarty->display('index.tpl');
?>
//引用類(lèi)文件
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
//設(shè)置各個(gè)目錄的路徑,這里是安裝的重點(diǎn)
$smarty->template_dir = "smarty/templates/templates";
$smarty->compile_dir = "smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir = "smarty/templates/cache";
//smarty模板有高速緩存的功能,如果這里是true的話即打開(kāi)caching,但是會(huì)造成網(wǎng)頁(yè)不立即更新的問(wèn)題,當(dāng)然也可以通過(guò)其他的辦法解決
$smarty->caching = false;
$hello = "Hello World!";
//賦值
$smarty->assign("hello",$hello);
//引用模板文件
$smarty->display('index.tpl');
?>
(3) 執(zhí)行index.php就能看到Hello World!了。
二. 賦值
在模板文件中需要替換的值用大括號(hào){}括起來(lái),值的前面還要加$號(hào)。例如{$hello}。這里可以是數(shù)組,比如{$hello.item1},{$hello.item2}…
而PHP源文件中只需要一個(gè)簡(jiǎn)單的函數(shù)assign(var , value)。
簡(jiǎn)單的例子:
*.tpl:
Hello,{$exp.name}! Good {$exp.time}
*.php:
$hello[name] = “Mr. Green”;
$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);
output:
Hello,Mr.Green! Good morning
三. 引用
網(wǎng)站中的網(wǎng)頁(yè)一般header和footer是可以共用的,所以只要在每個(gè)tpl中引用它們就可以了。
示例:*.tpl:
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}
四. 判斷
模板文件中可以使用if else等判斷語(yǔ)句,即可以將一些邏輯程序放在模板里。"eq", "ne", "neq", "gt", "lt", "lte", "le", "gte" "ge", "is even", "is odd", "is not even", "is not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "<","<=",">="這些是if中可以用到的比較??纯淳湍苤朗裁匆馑及?。
示例:
{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
五. 循環(huán)
在Smarty里使用循環(huán)遍歷數(shù)組的方法是section,如何賦值遍歷都是在模板中解決,php源文件中只要一個(gè)assign就能解決問(wèn)題。
示例:
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
六. 常見(jiàn)問(wèn)題
Smarty將所有大括號(hào){}里的東西都視為自己的邏輯程序,于是我們?cè)诰W(wǎng)頁(yè)中想插入javascript函數(shù)就需要literal的幫忙了,literal的功能就是忽略大括號(hào){}。
示例:
{literal}
<script language=javascript>
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
</script>
{/literal}
您可能感興趣的文章:
- 在smarty模板中使用PHP函數(shù)的方法
- 模板引擎smarty工作原理以及使用示例
- 解析smarty模板中類(lèi)似for的功能實(shí)現(xiàn)
- smarty模板中使用get、post、request、cookies、session變量的方法
- PHP模板引擎Smarty的緩存使用總結(jié)
- PHP模板引擎smarty詳細(xì)介紹
- smarty模板引擎中內(nèi)建函數(shù)if、elseif和else的使用方法
- smarty模板引擎中自定義函數(shù)的方法
- php之Smarty模板使用方法示例詳解
- php Smarty模板生成html文檔的方法
- PHP 基于Yii框架中使用smarty模板的方法詳解
- smarty模板的使用方法實(shí)例分析
相關(guān)文章
php Http_Template_IT類(lèi)庫(kù)進(jìn)行模板替換
php Http_Template_IT模板替換實(shí)現(xiàn)代碼2009-03-03php smarty 二級(jí)分類(lèi)代碼和模版循環(huán)例子
分享下最近寫(xiě)的smarty模版引擎輸出二級(jí)分類(lèi)代碼,主要是靠二維數(shù)組進(jìn)行控制輸出。2011-06-06在PHP模板引擎smarty生成隨機(jī)數(shù)的方法和math函數(shù)詳解
如果要在smarty模板中生成隨機(jī)數(shù),該如何辦呢?在php文件中生成然后賦值到模板中。2014-04-04Pain 全世界最小最簡(jiǎn)單的PHP模板引擎 (普通版)
PHP全世界最小最簡(jiǎn)單的模板引擎------Pain (普通版),不同于smarty,所有的變量以{@ @}括起來(lái),有助于大家對(duì)模板引擎的了解。2011-10-10