Variable modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by the | (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifer name and are separated by : (colon).
變數修飾字(modifier)可以應用在變數、客制函數、或字串上。使用修飾字時,請指 定變數名稱之後緊接著一個 |(pipe)符號然後再接著一個修飾字名稱。修飾字可以增加參數以改變行為,參數的使用方法 為加在在修飾字後面並使用 :(colon)作分隔.
If you apply a modifier to an array variable instead of a single value variable, the modifier will be applied to every value in that array. If you really want the modifier to work on an entire array as a value, you must prepend the modifier name with an @ symbol like so: {$articleTitle|@count} (this will print out the number of elements in the $articleTitle array.)
如果你對陣列變數而不是單一變數使用修飾字的話,修飾字將會作用在陣列內的所有變數。如果 你希望修飾字將整個陣列視為一個變數的話,則可以在修飾字前加上 @ 符號,如: {$articleTitle|@count} (此用法將會列印出 $articleTitle 陣列內的元素個數。)
Modifiers can be autoloaded from your $plugins_dir (also see: Naming Conventions) or can be registered explicitely (see: register_modifier). Additionally all php-functions can be used as modifiers implicitly. (The @count-example above actually uses php's count-function and not a smarty-modifier). Using php-functions as modifiers has two little pitfalls: First: Sometimes the order of the function-parameters is not the desirable one ({"%2.f"|sprintf:$float} actually works, but asks for the more intuitive. For example:{$float|string_format:"%2.f"} that is provided by the Smarty distribution). Second: with $security turned on all php-functions that are to be used as modifiers have to be declared trusted in the $security_settings['MODIFIER_FUNCS']-array.
修飾字會由你的 $plugins_dir(也請參閱: Naming Conventions)自動載入,或是你也可以註冊修飾字(請參閱:register_modifier),此外,所有的 PHP 函式都可以當成修飾字。(上頭的 @count 範例實際上是使用 PHP 的 count 函式,而非 smarty 修飾字)。使用 PHP 函式作為修飾字 有兩個容易出錯的狀況:第一:有時函式參數的順序並不是我們所習以為常的({"%2.f"|sprintf:$float} 實際上是可以運作的,但是並不直觀。如:{$float|string_format:"%2.f"} 是由 Smarty 套件所提供的功能)。第二:當 $security 變數打開之後,如果想使用 PHP 函式作為修飾字的話,則必須在 $security_settings['MODIFIER_FUNCS'] 陣列中宣告信任。
| Parameter Position | Type | Required | Default | Description |
|---|---|---|---|---|
| 1 | boolean | No | false | This determines whether or not words with digits will be uppercased |
This is used to capitalize the first letter of all words in a variable.
此函式是把單字的第一個字元變成大寫。
Example 5-2. capitalize
Where index.tpl is: 其中 index.tpl 的內容為:
This will output: 輸出結果為:
|