{assign}鐢ㄤ簬鍦妯℃澘杩愯鏈熼棿璧嬪肩粰鍙橀噺.
Note
鍦ㄦā鏉夸腑杩涜璧嬪硷紝浠庢牴鏈笂璁茶繕鏄皢绋嬪簭閫昏緫鏀惧埌鏄剧ず灞傛潵杩涜浜嗭紝鍦≒HP绔繘琛屾鎿嶄綔浼氭洿濂姐傝鑷鑰冭檻銆
Note
鍙傝璧嬪肩粰鍙橀噺鏂规硶鐨缂╁啓銆
灞炴:
| 鍙傛暟鍚嶇О | 绫诲瀷 | 蹇呴夊弬鏁 | 榛樿鍊 | 璇存槑 |
|---|---|---|---|---|
| var | string | Yes | n/a | 琚祴鍊肩殑鍙橀噺鍚 |
| value | string | Yes | n/a | 璧嬬殑鍊 |
| scope | string | No | n/a | 鍙橀噺鐨勪綔鐢ㄨ寖鍥: 'parent','root' 鎴 'global' |
鍙夋爣璁:
| 鍚嶇О | 璇存槑 |
|---|---|
| nocache | 瀵硅祴鍊兼搷浣滀笉杩涜缂撳瓨 |
Example聽7.8.聽{assign}
{assign var="name" value="Bob"}
{assign "name" "Bob"} {* short-hand *}
The value of $name is {$name}.
杈撳嚭锛
The value of $name is Bob.
Example聽7.9.聽{assign} 浣跨敤nocache灞炴
{assign var="name" value="Bob" nocache}
{assign "name" "Bob" nocache} {* short-hand *}
The value of $name is {$name}.
杈撳嚭锛
The value of $name is Bob.
Example聽7.10.聽{assign} 杩涜鏁板杩愮畻
{assign var=running_total value=$running_total+$some_array[$row].some_value}
Example聽7.11.聽{assign} 鍦ㄨ皟鐢ㄧ殑妯℃澘鍐呯殑浣滅敤鑼冨洿
鍦ㄥ寘鍚殑妯℃澘鍐呰祴鍊肩殑鍙橀噺锛屽湪鍖呭惈妯℃澘鍐呭彲瑙併
{include file="sub_template.tpl"}
...
{* display variable assigned in sub_template *}
{$foo}<br>
...
涓婇潰鐨勬ā鏉挎槸鍖呭惈浜嗕笅闈㈢殑妯℃澘sub_template.tpl
...
{* foo will be known also in the including template *}
{assign var="foo" value="something" scope=parent}
{* bar is assigned only local in the including template *}
{assign var="bar" value="value"}
...
Example聽7.12.聽{assign} 浣滅敤鑼冨洿渚嬪瓙
璁剧疆鍙橀噺璁块棶鑼冨洿涓簉oot锛岀劧鍚庤鍙橀噺鍦ㄧ浉鍏虫ā鏉块噷闈㈤兘鍙銆
{assign var=foo value="bar" scope="root"}
Example聽7.13.聽{assign} 璧嬪间竴涓叏灞鍙橀噺
鍏ㄥ眬鍙橀噺鍦ㄤ换浣曟ā鏉垮唴鍧囧彲瑙併
{assign var=foo value="bar" scope="global"}
{assign "foo" "bar" scope="global"} {* short-hand *}
Example聽7.14.聽浠嶱HP鑴氭湰涓幏鍙杮assign} 鐨勫彉閲
鍙互浣跨敤
getTemplateVars()鍦≒HP鑴氭湰涓幏鍙{assign}鐨勫彉閲忓笺
杩欓噷鐨勬ā鏉垮垱寤轰簡鍙橀噺$foo銆
{assign var="foo" value="Smarty"}
褰撴ā鏉胯鎵ц鏃/鎵ц鍚庯紝鍙互鐢ㄤ互涓嬬殑鏂瑰紡鍦≒HP鑾峰彇鍒拌繖涓ā鏉垮彉閲忋
<?php
// this will output nothing as the template has not been executed
echo $smarty->getTemplateVars('foo');
// fetch the template to a variable
$whole_page = $smarty->fetch('index.tpl');
// this will output 'smarty' as the template has been executed
echo $smarty->getTemplateVars('foo');
$smarty->assign('foo','Even smarter');
// this will output 'Even smarter'
echo $smarty->getTemplateVars('foo');
?>
涓嬮潰鐨勫嚱鏁伴兘鍙敤assign浣滀负鍙灞炴э紝鐢ㄤ簬灏嗗嚱鏁扮粨鏋滆祴鍊肩粰鍙橀噺鑰屼笉鏄剧ず銆
{capture},
{include},
{include_php},
{insert},
{counter},
{cycle},
{eval},
{fetch},
{math},
{textformat}
鍙傝
{$var=...},
assign()
鍜
getTemplateVars().
