Smarty 入門

2016-02-19 16:50 0 1 收藏

下面是个简单易学的Smarty 入門教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - Web开发 】

  序言

!-- START : Block name --

  區域內容

  !-- END : Block name --

  

  這些區塊大部份都會在 PHP 程式中以 if 或 for, while 來控制它們的顯示狀態,雖然樣版看起來簡潔多了,但只要一換了顯示方式不同的樣版, PHP 程式勢必要再改一次!main.php:

?php  include "class/Smarty.class.php";  define('__SITE_ROOT', 'd:/appserv/web/demo'); // 最後沒有斜線  $tpl = new Smarty();  $tpl-template_dir = __SITE_ROOT . "/templates/";  $tpl-compile_dir = __SITE_ROOT . "/templates_c/";  $tpl-config_dir = __SITE_ROOT . "/configs/";  $tpl-cache_dir = __SITE_ROOT . "/cache/";  $tpl-left_delimiter = '{';  $tpl-right_delimiter = '}';?

  照上面方式設定的用意在於,程式如果要移植到其他地方,只要改 __SITE_ROOT 就可以啦。 (這裡是參考 XOOPS 的 )templates/test.htm:

htmlheadmeta http-equiv="Content-Type" content="text/html; charset=big5"title{$title}/title/headbody{$content}/body/html

  現在我們要將上面的樣版顯示出來,並將網頁標題 ($title) 與內容 ($content) 更換,請將以下檔案內容命名為 test.php ,並放置在主資料夾下:test.php:

?php  require "main.php";  $tpl-assign("title", "測試用的網頁標題");  $tpl-assign("content", "測試用的網頁內容");  // 上面兩行也可以用這行代替  // $tpl-assign(array("title" = "測試用的網頁標題", "content" = "測試用的網頁內容"));  $tpl-display('test.htm');?templates_c/%%179/%%1798044067/test.htm.php:?php /* Smarty version 2.6.0, created on 2003-12-15 22:19:45 compiled from test.htm */ ?htmlheadmeta http-equiv="Content-Type" content="text/html; charset=big5"title?php echo $this-_tpl_vars['title']; ?/title/headbody?php echo $this-_tpl_vars['content']; ?/body/html

  沒錯,這就是 Smarty 編譯過的檔案。它將我們在樣版中的變數轉換成了 PHP 的語法來執行,下次再讀取同樣的內容時, Smarty 就會直接抓取這個檔案來執行了。main.php:

?php    include   "class/Smarty.class.php";    define(  '__SITE_ROOT', 'd:/appserv/web/demo'); // 最後沒有斜線  // 以 main.php 的位置為基準    require_once   "includes/functions.php";    require_once   "includes/include.php";  $tpl = new Smarty();    $tpl-  template_dir = __SITE_ROOT . "/templates/";    $tpl-  compile_dir = __SITE_ROOT . "/templates_c/";    $tpl-  config_dir = __SITE_ROOT . "/configs/";    $tpl-  cache_dir = __SITE_ROOT . "/cache/";    $tpl-  left_delimiter = '{';    $tpl-  right_delimiter = '}';?

  modules 這個資料夾則是用來放置程式模組的,如此一來便不會把程式丟得到處都是,整體架構一目瞭然。

  1. {$var}

  2. { $var } !-- 和變數之間有空格 --

  3. {$var} !-- 啟始的標示符號和結束的標示符號不在同一行 --

  在 Smarty 裡,變數預設是全域的,也就是說你只要指定一次就好了。指定兩次以上的話,變數內容會以最後指定的為主。就算我們在主樣版中載入了外部的子樣版,子樣版中同樣的變數一樣也會被替代,這樣我們就不用再針對子樣版再做一次解析的動作。{變數|修飾函式} !-- 當修飾函式沒有參數時 --

  {變數|修飾函式:"參數(非必要,視函式而定)"} !-- 當修飾函式有參數時 --

  

  範例如下:

{$var|nl2br} !-- 將變數中的換行字元換成 br / --{$var|string_format:"%02d"} !-- 將變數格式化 --input name="total" type="hidden" value="21000" /

  總金額:21,000 元

  

  一般樣版引擎的樣版可能會這樣寫:input name="total" type="hidden" value="{total}" /

  總金額:{format_total} 元

  

?php    $total = 21000;    $tpl-assign("total", $total);    $tpl-assign("format_total", number_format($total));?

  而 Smarty 的樣版就可以這樣寫: (number_format 修飾函式請到 Smarty 官方網頁下載)input name="total" type="hidden" value="{$total}" /

  總金額:{$total|number_format:""} 元

  

?php    $total = 21000;    $tpl-assign("total", $total);?

  所以在 Smarty 中我們只要指定一次變數,剩下的交給樣版自行決定即可。這樣瞭解了嗎?這就是讓樣版自行決定變數呈現風貌的好處!

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  控制樣版的內容

  重覆的區塊 test2.php:

?php    require "main.php";    $array1 = array(1 = "蘋果", 2 = "鳳梨", 3 = "香蕉", 4 = "芭樂");    $tpl-assign("array1", $array1);    $array2 = array(    array("index1" = "data1-1", "index2" = "data1-2", "index3" = "data1-3"),    array("index1" = "data2-1", "index2" = "data2-2", "index3" = "data2-3"),    array("index1" = "data3-1", "index2" = "data3-2", "index3" = "data3-3"),    array("index1" = "data4-1", "index2" = "data4-2", "index3" = "data4-3"),    array("index1" = "data5-1", "index2" = "data5-2", "index3" = "data5-3"));    $tpl-assign("array2", $array2);    $tpl-display("test2.htm");?

  而樣版的寫法如下:templates/test2.htm:

htmlheadmeta http-equiv="Content-Type" content="text/html; charset=big5"title測試重覆區塊/title/headbodypre利用 foreach 來呈現 array1{foreach item=item1 from=$array1}{$item1}{/foreach}利用 section 來呈現 array1{section name=sec1 loop=$array1}{$array1[sec1]}{/section}利用 foreach 來呈現 array2{foreach item=index2 from=$array2}{foreach key=key2 item=item2 from=$index2}{$key2}: {$item2}{/foreach}{/foreach}利用 section 來呈現 array1{section name=sec2 loop=$array2}index1: {$array2[sec2].index1}index2: {$array2[sec2].index2}index3: {$array2[sec2].index3}{/section}/pre/body/htmltest3.php:?php  require "main.php";  $forum = array(    array("category_id" = 1, "category_name" = "公告區",      "topic" = array(        array("topic_id" = 1, "topic_name" = "站務公告")      )    ),    array("category_id" = 2, "category_name" = "文學專區",      "topic" = array(        array("topic_id" = 2, "topic_name" = "好書介紹"),        array("topic_id" = 3, "topic_name" = "奇文共賞")      )    ),    array("category_id" = 3, "category_name" = "電腦專區",      "topic" = array(        array("topic_id" = 4, "topic_name" = "硬體週邊"),        array("topic_id" = 5, "topic_name" = "軟體討論")      )    )  );  $tpl-assign("forum", $forum);  $tpl-display("test3.htm");?

  樣版的寫法如下:templates/test3.htm:

htmlheadtitle巢狀迴圈測試/title/headbodytable width="200" border="0" align="center" cellpadding="3" cellspacing="0"  {section name=sec1 loop=$forum}  tr    td colspan="2"{$forum[sec1].category_name}/td  /tr  {section name=sec2 loop=$forum[sec1].topic}  tr    td width="25" /td    td width="164"{$forum[sec1].topic[sec2].topic_name}/td  /tr  {/section}  {/section}/table/body/html

  test3.php:

?php  require "main.php";  // 先建立第一層陣列  $category = array();  $db-setSQL($SQL1, 'CATEGORY');  if (!$db-query('CATEGORY')) die($db-error());  // 抓取第一層迴圈的資料  while ($item_category = $db-fetchAssoc('CATEGORY'))  {    // 建立第二層陣列    $topic = array();    $db-setSQL(sprintf($SQL2, $item_category['category_id']), 'TOPIC');    if (!$db-query('TOPIC')) die($db-error());    // 抓取第二層迴圈的資料    while ($item_topic = $db-fetchAssoc('TOPIC'))    {      // 把抓取的資料推入第二層陣列中      array_push($topic, $item_topic);    }    // 把第二層陣列指定為第一層陣列所抓取的資料中的一個成員    $item_category['topic'] = $topic;    // 把第一層資料推入第一層陣列中    array_push($category, $item_category);  }  $tpl-assign("forum", $category);  $tpl-display("test3.htm");?

  在資料庫抓取一筆資料後,我們得到的是一個包含該筆資料的陣列。透過 while 敘述及 array_push 函式,我們將資料庫中的資料一筆一筆塞到陣列裡。如果您只用到單層迴圈,就把第二層迴圈 (紅色的部份) 去掉即可。

  決定內容是否顯示 {if $is_login == true}

  顯示使用者操作選單

  {else}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  顯示輸入帳號和密碼的表單

  {/if}

  

  要注意的是,「==」號兩邊一定要各留至少一個空白字元,否則 Smarty 會無法解析。test4.php:

?php  require "main.php";  $my_array = array(    array("value" = "0"),    array("value" = "1"),    array("value" = "2"),    array("value" = "3"),    array("value" = "4"),    array("value" = "5"),    array("value" = "6"),    array("value" = "7"),    array("value" = "8"),    array("value" = "9"));  $tpl-assign("my_array", $my_array);  $tpl-display('test4.htm');?

  樣版的寫法如下:templates/test4.htm:

htmlheadtitle橫向重覆表格測試/title/headbodytable width="500" border="1" cellspacing="0" cellpadding="3"  tr  {section name=sec1 loop=$my_array}    td{$my_array[sec1].value}/td  {if $smarty.section.sec1.rownum is div by 2}  /tr  tr  {/if}  {/section}  /tr/table/body/htmltest5.php:?php  require "main.php";  $tpl-assign("title", "Include 測試");  $tpl-assign("content", "這是樣版 2 中的變數");  $tpl-assign("dyn_page", "test5_3.htm");  $tpl-display('test5_1.htm');?

  樣版 1 的寫法如下:templates/test5_1.htm:

htmlheadmeta http-equiv="Content-Type" content="text/html; charset=big5"title{$title}/title/headbody{include file="test5_2.htm"}br /{include file=$dyn_page}{include file="test5_4.htm" custom_var="自訂變數的內容"}/body/htmltemplates/test5_2.htm:{$content}

  樣版 3 的寫法如下:templates/test5_3.htm:這是樣版 3 的內容

  templates/test5_4.htm:{$custom_var}

  

  這裡注意幾個重點:1. 樣版的位置都是以先前定義的 template_dir 為基準;2. 所有 include 進來的子樣版中,其變數也會被解譯。;3. include 中可以用「變數名稱=變數內容」來指定引含進來的樣版中所包含的變數,如同上面樣版 4 的做法。

来源:https://www.tulaoshi.com/n/20160219/1613267.html

延伸阅读
每个胎宝宝在子宫内发育情况不同,因此入盆的时间也就有早有晚。在36周左右,孕妇务必要进行产检,以此来确定胎宝宝是否入盆。那么胎儿一般何时入盆?胎儿入盆是什么感觉?胎儿不入盆该怎么办? 什么是胎儿入盆 在孕3...
入园经:给孩子时间去适应 宝宝姓名:二娃 宝宝生日:2006-06-09 二二是09年4月开始上俊华双语幼儿园的。之前我带她参加了一个为两~三岁小孩设立的Mom-Tots班,相当于国内所说的亲子班。这个班只是在星期一,三活动两小时。二二在亲子班上认识了两个和她年龄相仿的ABC小女孩,三人就成为好朋友了。我和她们的妈妈也常一起...
入园适应问答系列“在园情况” 小编整理了摇篮专家在线访谈和育儿问答中,关于“在园情况”的问题的精彩问答,希望对初入园的宝宝和妈妈们有所帮助。 Q 图老师育儿网育儿问答小编:如何知晓孩子在幼儿园的表现 把宝宝送到幼儿园后,妈妈最关心的是在幼儿园里的表现了,是一个跟在家里一样活泼的宝宝还是一个继续内...
入园年龄 小班孩子的入园年龄应该满3周岁。因为在这个年龄阶段的孩子,已经具备了基本的语言能力和初具独立生活的能力。有些幼儿园还设置了小小班,小小班的入园年龄则应该在2岁至2岁半未满3岁的孩子。 入园报名 幼儿园每年都在春季公开招生,招生的范围一般是在幼儿园所在地区的周围。 孩子入园的报名时间一般在6月左右,也有些幼儿园会...
入园面试入园前的特殊准备 入园面试准备 幼儿园也会有一个简单的对宝贝的面试。面试最主要的目的就是要了解一下宝贝的情况,让宝贝、家长和老师有一个彼此熟悉和沟通的机会。这个面试没 有我们想象的那么严格,所以没必要过分担心。给宝贝报名后,被通知去参加面试的都是有大致意向的,只要不是智力上有什么大的缺陷,一般都能通过。 幼...

经验教程

879

收藏

17

精华推荐

入园记

入园记

love幸福达人

入园经:宝宝要入园妈妈先过关

入园经:宝宝要入园妈妈先过关

九五六月三

胆小勿入

胆小勿入

GH优雅小女人

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部