Variables
Template variables start with a dollar sign. They can contain numbers,
letters and underscores, much like a PHP variable. You can reference arrays
that are indexed numerically or non-numerically. You can also reference
object properties and methods. Config file variables are an exception to the
dollar sign syntax. They can be referenced with surrounding hashmarks, or
with the special $smarty.config variable.
樣版變數之前會有一個金錢符號,變數名稱可以包括數字、英文、與底線,與 PHP 的
變數命名方式類似。你可以參照數字索引的陣列,也可以參考到文字索引的陣列,你也可以參照
物件的屬性與函式。組態檔的變數前會有一個金錢符號,我們也可以使用由井字號包圍的變數或
$smarty.config 變數參照到組態檔的變數。
Example 3-2. 變數 {$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}
Many other combinations are allowed
{$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 |
|