Smarty變數用法詳解_php執行個體

來源:互聯網
上載者:User
本文執行個體講述了Smarty變數用法。分享給大家供大家參考,具體如下:

1. 從PHP分配的變數

調用從PHP分配的變數需在前加"$"符號.(譯註:同php一樣)

調用模板內的assign函數分配的變數也是這樣.(譯註:也是用$加變數名來調用)

樣本:

index.php:

$smarty = new Smarty;$smarty->assign('firstname', 'Doug');$smarty->assign('lastLoginDate', 'January11th, 2001');$smarty->display('index.tpl');

index.tpl:

Hello {$firstname}, glad to see you couldmake it.

Your last login was on {$lastLoginDate}.

輸出:

Hello Doug, glad to see you could make it.

Your last login was on January 11th, 2001.

2. 從設定檔讀取的變數

設定檔中的變數需要通過用兩個"#"或者是smarty的保留變數 $smarty.config.來調用(後面會講到)

第二種文法在變數作為屬性值並被引號括住的時候非常有用.

(譯註:舉個例子 {include file="#includefile#"} 這樣#includefile#將被當作字元處理,而不表示設定檔變數,但可以這樣表示{include file="`$smarty.config.includefile`"}不要忘了加``)

樣本:

foo.conf:

pageTitle = "This is mine"bodyBgColor = "#eeeeee"tableBorderSize = "3"tableBgColor = "#bbbbbb"rowBgColor = "#cccccc"

index.tpl:

{config_load file="foo.conf"}{#pageTitle#}
 
First Last Address

index.tpl:

{config_load file="foo.conf"}{$smarty.config.pageTitle}
 
First Last Address

上述兩種模板寫法都輸出:

This is mine
 
First Last Address

設定檔的變數只有在它們被載入以後才能使用.

更多關於Smarty相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於smarty模板的PHP程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.