PHPマニュアル/PEARマニュアル | ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot

include_php | JavaScript入門&応用&リファレンスなら「JavaScriptist」

  

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

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 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.

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.

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.

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

Example 7-9. function 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}
忘却曲線を使ってこの関数を確実に記憶に残す

フォーラムで「include_php」について話す
各種マニュアル: PHPマニュアル | PEARマニュアル | Smarty(英語)マニュアル | PHP-GTKマニュアル | include_php」をGoogle検索
copyright © 1997-2024 PHP ドキュメント作成グループ(ライセンス). provided by php spot. マニュアル: