diff --git a/Smarty.class.php b/Smarty.class.php index cc3b139e..4091187f 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -1,4 +1,5 @@ $arg_value"; } + if (!empty($attrs['assign'])) { + $return = "_tpl_vars;\n" . + "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . + "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . + "unset(\$_smarty_tpl_vars);\n"; + $return .= "\$this->assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>"; + } else { + $return = "_tpl_vars;\n" . + "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . + "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . + "unset(\$_smarty_tpl_vars); ?>"; + } + return $return; - return "_tpl_vars;\n" . - "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . - "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . - "unset(\$_smarty_tpl_vars); ?>"; } /*======================================================================*\ @@ -456,8 +467,8 @@ class Smarty_Compiler extends Smarty { return false; } + $this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name); if ($this->security) { - $this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name); if( $resource_type != 'file' || !@is_file($resource_name)) { $this->_syntax_error("include_php: $resource_type: $resource_name is not readable"); return false; @@ -467,8 +478,15 @@ class Smarty_Compiler extends Smarty { return false; } } - - return ""; + + if (!empty($attrs['assign'])) { + $return = "assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>"; + } else { + $return = ""; + } + return $return; } diff --git a/docs.sgml b/docs.sgml index 83c37c24..8048318c 100644 --- a/docs.sgml +++ b/docs.sgml @@ -2306,8 +2306,7 @@ pass=foobar Include tags are used for including other templates in the current template. Any variables available in the current template are also available within the included template. The include tag must have - the attribute "file", which contains the path to the included - template file relative to the template directory. + the attribute "file", which contains the template resource path. function include @@ -2337,6 +2336,86 @@ pass=foobar {include file="footer.tpl" logo="http://my.domain.com/logo.gif"} + + + + + include_php + + + + + + + + + + Attribute Name + Type + Required + Default + Description + + + + + file + string + Yes + n/a + The name of the php file to include + + + + + + 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. + + + include_php was added to Smarty 1.5.0. + + +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} + diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index cc3b139e..4091187f 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -1,4 +1,5 @@ $arg_value"; } + if (!empty($attrs['assign'])) { + $return = "_tpl_vars;\n" . + "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . + "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . + "unset(\$_smarty_tpl_vars);\n"; + $return .= "\$this->assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>"; + } else { + $return = "_tpl_vars;\n" . + "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . + "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . + "unset(\$_smarty_tpl_vars); ?>"; + } + return $return; - return "_tpl_vars;\n" . - "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . - "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . - "unset(\$_smarty_tpl_vars); ?>"; } /*======================================================================*\ @@ -456,8 +467,8 @@ class Smarty_Compiler extends Smarty { return false; } + $this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name); if ($this->security) { - $this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name); if( $resource_type != 'file' || !@is_file($resource_name)) { $this->_syntax_error("include_php: $resource_type: $resource_name is not readable"); return false; @@ -467,8 +478,15 @@ class Smarty_Compiler extends Smarty { return false; } } - - return ""; + + if (!empty($attrs['assign'])) { + $return = "assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>"; + } else { + $return = ""; + } + return $return; }