Smarty 入門

你说的幸福是影

你说的幸福是影

2016-02-19 16:50

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

  序言

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

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

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

  

  範例如下:

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

  總金額:21,000 元

  

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

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

  總金額:{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 中我們只要指定一次變數,剩下的交給樣版自行決定即可。這樣瞭解了嗎?這就是讓樣版自行決定變數呈現風貌的好處!

  控制樣版的內容

  重覆的區塊 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}

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

  {/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 的做法。

展开更多 50%)
分享

猜你喜欢

Smarty 入門

Web开发
Smarty 入門

Smarty 学习随记!

PHP
Smarty 学习随记!

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

smarty学习笔记

PHP
smarty学习笔记

斗門嫂子面的家常做法

五花肉
斗門嫂子面的家常做法

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

smarty实例教程

PHP
smarty实例教程

Smarty实例教学 实例篇

Web开发
Smarty实例教学 实例篇

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

XML 编程思想:以 XML 形式提供 WordNet

XML 编程思想:以 XML 形式提供 WordNet

XML 编程思想: 学习对象元数据

XML 编程思想: 学习对象元数据
下拉加载更多内容 ↓