Attribute Name | Type | Required | Default | 描述 |
---|---|---|---|---|
file | string | Yes | n/a | The name of the php file to include |
once | boolean | No | true | whether or not to include the php file more than once if included multiple times |
assign | string | No | n/a | The name of the variable that the output of include_php will be assigned to |
屬性 | 類型 | 是否必須 | 缺省值 | 描述 |
---|---|---|---|---|
file | string | Yes | n/a | 待包含php文件的名稱 |
once | boolean | No | true | 如果待包含php文件已被包含是否仍然包含(類似php中的include_once函數(shù)) |
assign | string | No | n/a | 該屬性指定一個變量保存待包含php文件的輸出 |
inluce_php 函數(shù)用于在模板中包含 php 腳本. 如果設(shè)置了安全模式,被包含的腳本必須位于 $trusted_dir 路徑下. include_php 函數(shù)必須設(shè)置 file 屬性,該屬性指明被包含 php 文件的路徑,可以是 $trusted_dir 的相對路徑,也可以是絕對路徑.
include_php 是解決模板部件化的好方法,它使得 php 代碼從模板文件中被分離出來. 舉個例子:假設(shè)有一個從數(shù)據(jù)庫中動態(tài)取出數(shù)據(jù)用于顯示站點(diǎn)導(dǎo)航的模板,你可以將得數(shù)據(jù)內(nèi)容的 php 邏輯部分分離出來保存在一個單獨(dú)的文件夾下,并在模板開始的位置包含該 php 腳本. 那么就可以在任何地方包含此模板而不用擔(dān)心之前數(shù)據(jù)庫信息是否已被程序取出.
即使是在模板中多次地調(diào)用 php 文件,默認(rèn)情況下它們只被包含一次. 你可以設(shè)置 once 屬性從而指明每次調(diào)用都重新包含該文件. 如果將 once 屬性設(shè)置為 false,每次調(diào)用該文件都將被重新包含.
如果設(shè)置了 assign 屬性,該屬性對應(yīng)的變量名用于保存待包含 php 的輸出,這樣待包含 php 文件的輸出就不會直接顯示了。
在待包含 php 文件中可以通過 $this 訪問 smarty 對象.
Example 7-9. function include_php
|