首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Smarty 常用模板語法速查手冊:11 大核心代碼段完整範例

     編輯:PHP 相關知識

Smarty 常用模板語法速查手冊

Smarty 是 PHP 經典模板引擎,本文整理 11 類最常用代碼段範例,涵蓋表單元素生成、計數器、迴圈控制、變數指派、遠端內容抓取、圖片輸出、日期時間選單、表格生成、數學運算、郵件連結、彈出視窗,可作為開發速查參考。

1. 表單元素生成

{* 下拉選單 *}
<select> 
    {html_options values=$cust_ids selected=$customer_id output=$cust_names} 
</select>

{* 複選框 *}
{html_checkboxes values=$cust_ids checked=$customer_id output=$cust_names separator="<br/>"}

{* 單選鈕 *}
{html_radios values=$cust_ids checked=$customer_id output=$cust_names separator="<br/>"}

參數:values=值陣列、selected/checked=預設選中、output=顯示文字陣列、separator=分隔符。

2. 計數器

{counter start=0 skip=2 print=true}

從 0 開始,每次遞增 2,直接輸出。

3. 迴圈交替樣式

{section loop=$data}
    <tr bgcolor="{cycle values='#eeeeee,#d0d0d0'}">
        <td>{$data[rows]}</td>
    </tr>
{/section}

cycle 輪替輸出指定值,常用於斑馬紋表格。

4. 變數指派

{assign var="name" value="Bob"}
The value of $name is {$name}. {* 輸出:The value of $name is Bob *}

5. 遠端內容抓取

{fetch file="https://www.myweather.com/68502/" assign="weather"}

支援 https://、ftp://、本地路徑,抓取內容存入變數。

6. 圖片輸出

{html_image file="../path/relative/to/currdir/pumpkin.jpg" 
    border="1" height="100" width="100" alt="圖片" href="1.html"}

7. 日期/時間選單

{html_select_date}  {* 年/月/日下拉 *}
{html_select_time}  {* 時/分/秒下拉 *}

8. 表格自動生成

{html_table loop=$data cols=4 table_attr='border="0"' tr_attr=$tr td_attr=$td}

將一維陣列按指定列數渲染為表格,支援 row/col 屬性自定義。

9. 數學運算

{math equation="x + y" x=$height y=$width}

10. 郵件連結

{mailto address="me@domain.com" text="send me some mail"}

11. JavaScript 彈出視窗

{popup} {* 需先呼叫 popup_init *}

熟悉這些核心語法,能大幅提升 Smarty 模板開發效率,減少 PHP 邏輯滲入視圖層。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved