Smarty:var函數(shù)

2018-10-14 11:05 更新

{$var=...}

{assign}函數(shù)的縮寫。你可以在模板內(nèi)對變量進(jìn)行賦值,或者對數(shù)組元素進(jìn)行賦值。

Note

在模板中進(jìn)行賦值,從根本上講還是將程序邏輯放到顯示層來進(jìn)行了,在PHP端進(jìn)行此操作會更好。請自行考慮。

下面是該標(biāo)簽的屬性:

屬性:

參數(shù)名稱縮寫類型必選參數(shù)默認(rèn)值說明
scopen/astringNon/a變量的作用范圍: 'parent','root' 或 'global'

可選標(biāo)記:

名稱說明
nocache對賦值操作不進(jìn)行緩存

Example 7.1. 簡單賦值

{$name='Bob'}

The value of $name is {$name}.

輸出:

The value of $name is Bob.

Example 7.2. 數(shù)學(xué)運(yùn)算賦值

{$running_total=$running_total+$some_array[row].some_value}

Example 7.3. 對數(shù)組元素賦值

{$user.name="Bob"}

Example 7.4. 對多維數(shù)組元素賦值

{$user.name.first="Bob"}

Example 7.5. 附加到數(shù)組

{$users[]="Bob"}

Example 7.6. 賦值的作用范圍

在包含的模板內(nèi)賦值的變量,在包含模板內(nèi)可見。

{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 *}
{$foo="something" scope=parent}
{* bar is assigned only local in the including template *}
{$bar="value"}
...
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號