include_php

Attribute NameTypeRequiredDefaultDescription
filestringYesn/aThe name of the php file to include
oncebooleanNotruewhether or not to include the php file more than once if included multiple times
assignstringNon/aThe name of the variable that the output of include_php will be assigned to

Technical Note: include_php is pretty much deprecated from Smarty, you can accomplish the same functionality via a custom template function. The only reason to use include_php is if you really have a need to quarantine the php function away from the plugin directory or your application code. See the componentized template example for details.

技術提醒: Smarty 不建議使用 include_php,你可以透過客制樣版函式完成相同的功能。唯 一建議使用 include_php 的時機是在當你真的需要從你的 plugin 目錄或程式碼中隔離出 php 函式時。請參閱 componentized template example

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.

include_php 標籤的功能是將 php 程式碼包含到你的樣版內,如果有使用 security 的話,則這些 php 程式碼必須在 $trusted_dir 路徑內。include_php 標籤必須有 "file" 屬性,且 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 程式 碼分散在各樣版檔中。舉個例子,假設你有一個樣版是要顯示你現在所瀏覽的位置,而值是從資 料庫中取得的,你可以在別的目錄的檔案中取得資料庫的內容,然後再將此樣版檔案包含在現在 樣版的頂端。現在你可以在任何一個樣版內包含這個樣版檔,而不用擔心在程式處理前就已經先 取得資料庫的資料了。

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.

預設狀況下,在同一個樣版內,即使使用多次 include 函式包含進某個 php 檔案,但實際上這個檔案也只會被包含進來一次,你可以在每次使用 include 函式包含檔案時 使用 once 參數,如果將 once 的值設為 false 的話則每次使用 include 函式時都會包含 php 檔一次 。

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.

你可以使用 assign 屬性,其值為使用 include_php 包含進來的(php)檔案其輸出的樣版檔案。

The smarty object is available as $this within the PHP script that you include.

我們可以在 PHP 程式檔中使用 $this 來代表 smarty 物件。

Example 7-9. include_php 函式

load_nav.php
-------------

<?php

	// load in variables from a mysql db and assign them to the template
	require_once("MySQL.class.php");
	$sql = new MySQL;
	$sql->query("select * from site_nav_sections order by name",SQL_ALL);
	$this->assign('sections',$sql->record);

?>


index.tpl
---------

{* absolute path, or relative to $trusted_dir *}
{include_php file="/path/to/load_nav.php"}

{foreach item="curr_section" from=$sections}
	<a href="{$curr_section.url}">{$curr_section.name}</a><br>
{/foreach}