?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
Attribute Name | Type | Required | Default | Description |
---|---|---|---|---|
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 | 該屬性指定一個(gè)變量保存待包含php文件的輸出 |
include_php tags are used to include a php script in your template. If security is enabled, then the php script must be located in the $trusted_dir path. The include_php tag must have the attribute "file", which contains the path to the included php file, either relative to $trusted_dir, or an absolute path.
inluce_php 函數(shù)用于在模板中包含 php 腳本. 如果設(shè)置了安全模式,被包含的腳本必須位于 $trusted_dir 路徑下. include_php 函數(shù)必須設(shè)置 file 屬性,該屬性指明被包含 php 文件的路徑,可以是 $trusted_dir 的相對路徑,也可以是絕對路徑.
include_php is a nice way to handle componentized templates, and keep PHP code separate from the template files. Lets say you have a template that shows your site navigation, which is pulled dynamically from a database. You can keep your PHP logic that grabs database content in a separate directory, and include it at the top of the template. Now you can include this template anywhere without worrying if the database information was assigned by the application before hand.
include_php 是解決模板部件化的好方法,它使得 php 代碼從模板文件中被分離出來. 舉個(gè)例子:假設(shè)有一個(gè)從數(shù)據(jù)庫中動(dòng)態(tài)取出數(shù)據(jù)用于顯示站點(diǎn)導(dǎo)航的模板,你可以將得數(shù)據(jù)內(nèi)容的 php 邏輯部分分離出來保存在一個(gè)單獨(dú)的文件夾下,并在模板開始的位置包含該 php 腳本. 那么就可以在任何地方包含此模板而不用擔(dān)心之前數(shù)據(jù)庫信息是否已被程序取出.
By default, php files are only included once even if called multiple times in the template. You can specify that it should be included every time with the once attribute. Setting once to false will include the php script each time it is included in the template.
即使是在模板中多次地調(diào)用 php 文件,默認(rèn)情況下它們只被包含一次. 你可以設(shè)置 once 屬性從而指明每次調(diào)用都重新包含該文件. 如果將 once 屬性設(shè)置為 false,每次調(diào)用該文件都將被重新包含.
You can optionally pass the assign attribute, which will specify a template variable name that the output of include_php will be assigned to instead of displayed.
如果設(shè)置了 assign 屬性,該屬性對應(yīng)的變量名用于保存待包含 php 的輸出,這樣待包含 php 文件的輸出就不會(huì)直接顯示了。
The smarty object is available as $this within the PHP script that you include.
在待包含 php 文件中可以通過 $this 訪問 smarty 對象.
Example 7-9. function include_php
|