W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Smarty有多種類型的變量。
在Smarty中的變量可以直接顯示,或者作為函數(shù),屬性and修飾器, 內(nèi)部條件表達(dá)式等的參數(shù)。 要顯示變量,可以簡(jiǎn)單地用定界符把變量括起來(lái)。
Example 4.1. 變量例子
{$Name} {$product.part_no} <b>{$product.description}</b> {$Contacts[row].Phone} <body bgcolor="{#bgcolor#}">
一個(gè)簡(jiǎn)單的檢查Smarty變量的方法是打開(kāi)Smarty的調(diào)試控制臺(tái)。
賦值的變量以美元符號(hào) ($
) 開(kāi)頭。
Example 4.2. 變量賦值
PHP 代碼
<?php $smarty = new Smarty(); $smarty->assign('firstname', 'Doug'); $smarty->assign('lastname', 'Evans'); $smarty->assign('meetingPlace', 'New York'); $smarty->display('index.tpl'); ?>
index.tpl
模板源碼:
Hello {$firstname} {$lastname}, glad to see you can make it. <br /> {* this will not work as $variables are case sensitive *} This weeks meeting is in {$meetingplace}. {* this will work *} This weeks meeting is in {$meetingPlace}.
輸出:
Hello Doug Evans, glad to see you can make it. <br /> This weeks meeting is in . This weeks meeting is in New York.
可以通過(guò)點(diǎn)號(hào)“.”來(lái)使用賦值的數(shù)組變量。
Example 4.3. 數(shù)組變量
<?php $smarty->assign('Contacts', array('fax' => '555-222-9876', 'email' => 'zaphod@slartibartfast.example.com', 'phone' => array('home' => '555-444-3333', 'cell' => '555-111-1234') ) ); $smarty->display('index.tpl'); ?>
index.tpl
模板代碼:
{$Contacts.fax}<br /> {$Contacts.email}<br /> {* you can print arrays of arrays as well *} {$Contacts.phone.home}<br /> {$Contacts.phone.cell}<br />
輸出:
555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br />
你可以通過(guò)下標(biāo)來(lái)使用數(shù)組,和PHP語(yǔ)法一樣。
Example 4.4. 使用數(shù)組下標(biāo)
<?php $smarty->assign('Contacts', array( '555-222-9876', 'zaphod@slartibartfast.example.com', array('555-444-3333', '555-111-1234') )); $smarty->display('index.tpl'); ?>
index.tpl
模板代碼:
{$Contacts[0]}<br /> {$Contacts[1]}<br /> {* you can print arrays of arrays as well *} {$Contacts[2][0]}<br /> {$Contacts[2][1]}<br />
輸出:
555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br />
從PHP賦值的對(duì)象的屬性和方法,可以通過(guò)->
來(lái)使用。
Example 4.5. 使用對(duì)象
name: {$person->name}<br /> email: {$person->email}<br />
輸出:
name: Zaphod Beeblebrox<br /> email: zaphod@slartibartfast.example.com<br />
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: