Smarty模板快速入門_PHP教程

來源:互聯網
上載者:User
在PHP的世界裡已經出現了各式各樣的模板類,但就功能和速度來說Smarty還是一直處於領先地位,因為Smarty的功能相對強大,所以使用起來比其他一些模板類稍顯複雜了一點。現在就用30分鐘讓您快速入門。

  一. 安裝

首先開啟網頁http://smarty.php.net/download.php,下載最新版本的Smarty。解壓下載的檔案(目錄結構還蠻複雜的)。接下來我示範給大家一個安裝執行個體,看過應該會舉一反三的。
(1) 我在根目錄下建立了新的目錄learn/,再在learn/裡建立一個目錄smarty/。將剛才解壓縮出來的目錄的libs/拷貝到smarty/裡,再在smarty/裡建立templates目錄,templates裡建立cache/,templates/,templates_c/, config/.

(2) 建立一個模板檔案:index.tpl,將此檔案放在learn/smarty/templates/templates目錄下,代碼如下:
複製代碼 代碼如下:




Smarty


{$hello}





建立index.php,將此檔案放在learn/下:
複製代碼 代碼如下://引用類檔案
require 'smarty/libs/Smarty.class.php';

$smarty = new Smarty;

//設定各個目錄的路徑,這裡是安裝的重點
$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的話即開啟caching,但是會造成網頁不立即更新的問題,當然也可以通過其他的辦法解決
$smarty->caching = false;

$hello = "Hello World!";
//賦值
$smarty->assign("hello",$hello);

//引用模板檔案
$smarty->display('index.tpl');

?>



(3) 執行index.php就能看到Hello World!了。

  二. 賦值

在模板檔案中需要替換的值用大括弧{}括起來,值的前面還要加$號。例如{$hello}。這裡可以是數組,比如{$hello.item1},{$hello.item2}…
而PHP源檔案中只需要一個簡單的函數assign(var , value)。
簡單的例子:
*.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

三. 引用
網站中的網頁一般header和footer是可以共用的,所以只要在每個tpl中引用它們就可以了。
樣本:*.tpl:
{include file="header.tpl"}

{* body of template goes here *}

{include file="footer.tpl"}

  四. 判斷
模板檔案中可以使用if else等判斷語句,即可以將一些邏輯程式放在模板裡。"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}


  五. 迴圈

在Smarty裡使用迴圈遍曆數組的方法是section,如何賦值遍曆都是在模板中解決,php源檔案中只要一個assign就能解決問題。
樣本:
{* this example will print out all the values of the $custid array *}

{section name=customer loop=$custid}

id: {$custid[customer]}

{/section}

OUTPUT:

id: 1000

id: 1001

id: 1002


  六. 常見問題

Smarty將所有大括弧{}裡的東西都視為自己的邏輯程式,於是我們在網頁中想插入javascript函數就需要literal的幫忙了,literal的功能就是忽略大括弧{}。
樣本:
{literal}

{/literal}

http://www.bkjia.com/PHPjc/317630.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/317630.htmlTechArticle在PHP的世界裡已經出現了各式各樣的模板類,但就功能和速度來說Smarty還是一直處於領先地位,因為Smarty的功能相對強大,所以使用起來比...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.