跳轉至內容

PHP 程式設計/Smarty 模板系統

來自華夏公益教科書

什麼是 Smarty?

[編輯 | 編輯原始碼]

Smarty 是一個用於 PHP 的模板引擎。它允許您透過將 PHP 程式碼與 HTML(或其他任何內容)表示分離來分離邏輯表示[1]

舊/自定義模板引擎示例

[編輯 | 編輯原始碼]

它就像下面這樣:(以下程式碼是自定義模板引擎的演示,不是 Smarty)

這裡有兩個檔案“mail.html”(我們可以說這是一個模板檔案)和“mail_engine.php”(是的,你說得對...這是一個引擎。)

mail.html
 <html>
 <body>
 <h1>My company name is #COMPANY#</h1>
 <p>
   Please note our address #ADDRESS1#, #ADDRESS2#, #CITY#-#PIN#.
   Contact us on #PHONE#.
 </p>
 <p>
  Hope you like my mail.
 </p>
 <p>
  Thanking You,
 </p>
 <address> Jaydeep Dave, +919898456445, jaydipdave@yahoo.com </address>
 </body>
 </html>
mail_engine.php
<?php
 $contents = file_get_contents("mail.html");
 function rtemplate(&$tdata,$vars)
 {
   $nv = array();
   foreach($vars as $key => $value)
   {
     $kk = "#".strtoupper($key)."#";
     $nv[$kk] = $value;
   }
   unset($vars);
   $tdata = strtr($nv,$tdata);
   return true;
 }
 $vars = array(
   "company" => "Premier Business Softwares&quot",
   "address1" => "XXXXXXXXXXX",
   "address2" => "XXXXXXXXXXX",
   "city"=>"bHAVNAGAR",
   "pin"=>"364001",
   "phone"=>"+919898456445"
 );
 rtemplate($contents,$vars);
 echo $contents;
?>

此示例允許少量功能。如果您的公司價值包含 #CITY#,例如,您可能會遇到很大的問題。使用 Smarty 的好處還有很多。但無論如何,原始 PHP 模板是最有效和最快的。

它是如何工作的?

[編輯 | 編輯原始碼]

Smarty 是一個模板引擎,它實際上將模板檔案編譯成 PHP 檔案,該檔案可以稍後執行。這簡單地節省了解析和變數輸出的時間,以更少的記憶體使用和正則表示式擊敗其他模板引擎。

安裝非常基本且易於使用

  1. smarty.net 下載 Smarty 原始碼
  2. 使用您的存檔提取器開啟它(必須與 .tar.gz 檔案相容)
  3. 轉到名為 Smarty-x.x.x 的目錄
  4. libs資料夾複製到您網站的根目錄(您希望網站存在的目錄,例如 /My_Site/)
  5. 您完成了!

不需要複製其他檔案,因為它們只是簡單的示例。

簡單示例

[編輯 | 編輯原始碼]
<?php
     $abc = 'hello ';
     $smarty->abc("abc",$abc);
?>
{$abc}

基本語法

[編輯 | 編輯原始碼]
 {* Sample Smarty Template *}
 
 {* Include a header file *}
 
 {* Include a file from a variable $header_file, which is defined by the php script *}
 {include file=$header_file}
 {include file="middle.tpl"}
 {* Simple alternative to PHP's echo $title;
 {$title}
 {* Include a file from a variable #footer#, which is defined by the config file *}
 {include file=#footer#}
 
 {* display dropdown lists *}
 <select name="company">
 {html_options values=$vals selected=$selected output=$output}
 </select>
 {*end*}

基本語法 #2

[編輯 | 編輯原始碼]

註釋

{* Comment *}

編寫一個變數,從 PHP 指令碼分配

{$variable}

編寫一個變數,從配置檔案分配

#variable#

在函式中使用變數

$variable

其他示例(Smarty 文件)

 {$foo}        <!-- displaying a simple variable (non array/object) -->
 {$foo[4]}     <!-- display the 5th element of a zero-indexed array -->
 {$foo.bar}    <!-- display the "bar" key value of an array, similar to PHP $foo['bar'] -->
 {$foo.$bar}   <!-- display variable key value of an array, similar to PHP $foo[$bar] -->
 {$foo->bar}   <!-- display the object property "bar" -->
 {$foo->bar()} <!-- display the return value of object method "bar" -->
 {#foo#}       <!-- display the config file variable "foo" -->
 {$smarty.config.foo} <!-- synonym for {#foo#} -->
 {$foo[bar]}   <!-- syntax only valid in a section loop, see {section}  -->

允許使用許多其他組合

 {$foo.bar.baz}
 {$foo.$bar.$baz}
 {$foo[4].baz}
 {$foo[4].$baz}
 {$foo.bar.baz[4]}
 {$foo->bar($baz,2,$bar)} <!-- passing parameters -->
 {"foo"}       <!-- static values are allowed -->

呼叫函式

{function}

整合到網站中

[編輯 | 編輯原始碼]

為了使用 Smarty 模板引擎,您需要更改您的 php 指令碼,該指令碼用於控制 Smarty 引擎並編譯模板檔案。簡單示例

<?php
 // Include Smarty Library
 require_once("libs/Smarty.inc.php");
 // Create new variable $smarty from the class Smarty
 $smarty=new Smarty();
 // Set the template directory, very useful for different templates
 $smarty->template_dir="templates";
 // From here you should put your own code
 // Set some variables
 $smarty->assign("Title"=>"Just a test");
 // Create an array, which we will assign later
 $contacts=array(
   array("Name"=>"John Parkinson","email"=>"john.parkinson.test@domain.tld","age"=>26),
   array("Name"=>"Super Mario","email"=>"super.mario@domain.tld","age"=>54),
   array("Name"=>"Pete Peterson","email"=>"pete.peterson@domain.tld","age"=>18),
   array("Name"=>"Smarty Creator","email"=>"smarty.creator@domain.tld","age"=>37)
 );
 // Assign the array
 $smarty->assign("contacts",$contacts);
 // Compile and Display output of the template file '''templates/index.tpl'''
 // Up to here you should put your own code
 $smarty->display("index.tpl");
?>

#基本語法 #2

請參閱 #變數

Smarty 中的迴圈就像 PHP 一樣,只是處理變數的方式不同。例如,在 PHP 中你會這樣寫

 foreach($array as $key => $value) {
    echo "$key => $value\n";
 }

Smarty 透過以下類似的程式碼片段來編譯

 {foreach from=$array key="key" item="value"}
 {$key} => {$value}
 {/foreach}

此外,您可以使用section函式,它與foreach非常類似

如果設計人員想要新增專案符號或陣列項的索引,程式設計師則無需執行任何操作,因為您可以使用其他變數,這些變數將在每次迴圈後自行更改。

Smarty 中的 {if} 語句與 PHP if 語句具有幾乎相同的靈活性,並且為模板引擎添加了一些額外的功能。每個 {if} 必須與 {/if} 配對。{else} 和 {elseif} 也允許。所有 PHP 條件語句都被識別,例如 ||、or、&&、and 等。

以下是公認限定符的列表,它們必須用空格與周圍的元素隔開。請注意,方括號中列出的專案是可選的。PHP 等效項在適用時顯示。

限定符 替代 語法示例 含義 PHP 等效項
== eq $a eq $b 等於 ==
!= ne, neq $a neq $b 不等於 !=
> gt $a gt $b 大於 >
< lt $a lt $b 小於 <
>= gte, ge $a ge $b 大於或等於 >=
<= lte, le $a le $b 小於或等於 <=
===   $a === 0 檢查同一性 ===
! not not $a 否定(一元) !
% mod $a mod $b 模運算 %
是否 [不] 能被整除   $a 不能被 4 整除 可被整除 $a % $b == 0
是否 [不] 為偶數   $a 不是偶數 [不] 為偶數 (一元) $a % 2 == 0
是否 [不] 能被整除為偶數   $a 不能被 $b 整除為偶數 分組級別是否 [不] 為偶數 ($a / $b) % 2 == 0
是否 [不] 為奇數   $a 不是奇數 [不] 為奇數 (一元) $a % 2 != 0
是否 [不] 能被整除為奇數   $a 不能被 $b 整除為奇數 [不] 為奇數分組 ($a / $b) % 2 != 0

參考資料

[編輯 | 編輯原始碼]
  1. Smarty 網站, 文件, 下載


華夏公益教科書