Smarty

一. 安裝
 
    首先打開網頁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目錄下,代碼如下:

<!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>
 新建index.php,將此文件放在learn/下:
<?php
//引用類文件
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]}<br>
{/section}
 
OUTPUT:
 
id: 1000<br>
id: 1001<br>
id: 1002<br>
六. 常見問題
 
       Smarty將所有大括號{}裏的東西都視爲自己的邏輯程序,於是我們在網頁中想插入javascript函數就需要literal的幫忙了,literal的功能就是忽略大括號{}。
       示例:
{literal}

       <script language=javascript>

             function isblank(field) {

                       if (field.value == '')

                               { return false; }

                       else

                               {

                               document.loginform.submit();

                               return true;

                               }

             }

       </script>
{/literal}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章