Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

{include_php}

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 plugins/ directory or your application code. See the componentized template example for details.

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

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

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.21. function {include_php}

The load_nav.php file:


<?php

// load in variables from a mysql db and assign them to the template
require_once('database.class.php');
$db = new Db();
$db->query('select url, name from navigation order by name');
$this->assign('navigation', $db->getRows());

?>

  

where the template is:


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

{foreach item='nav' from=$navigation}
  <a href="{$nav.url}">{$nav.name}</a><br />
{/foreach}

  

See also {include}, $security, $trusted_dir, {php}, {capture}, template resources and componentized templates