mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
*** empty log message ***
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Project: Smarty: the PHP compiling template engine
|
||||
* File: Smarty.class.php
|
||||
|
@@ -435,13 +435,24 @@ class Smarty_Compiler extends Smarty {
|
||||
$arg_value = $arg_value ? 'true' : 'false';
|
||||
$arg_list[] = "'$arg_name' => $arg_value";
|
||||
}
|
||||
|
||||
return "<?php " .
|
||||
if (!empty($attrs['assign'])) {
|
||||
$return = "<?php ob_start();\n";
|
||||
$return .=
|
||||
"\$_smarty_tpl_vars = \$this->_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 = "<?php " .
|
||||
"\$_smarty_tpl_vars = \$this->_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;
|
||||
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_include_php_tag
|
||||
@@ -456,8 +467,8 @@ class Smarty_Compiler extends Smarty {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->security) {
|
||||
$this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name);
|
||||
if ($this->security) {
|
||||
if( $resource_type != 'file' || !@is_file($resource_name)) {
|
||||
$this->_syntax_error("include_php: $resource_type: $resource_name is not readable");
|
||||
return false;
|
||||
@@ -468,7 +479,14 @@ class Smarty_Compiler extends Smarty {
|
||||
}
|
||||
}
|
||||
|
||||
return "<?php include('".$resource_name."'); ?>";
|
||||
if (!empty($attrs['assign'])) {
|
||||
$return = "<?php ob_start();\n";
|
||||
$return .= "include('".$resource_name."');\n";
|
||||
$return .= "\$this->assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>";
|
||||
} else {
|
||||
$return = "<?php include('".$resource_name."'); ?>";
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
|
83
docs.sgml
83
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.
|
||||
</para>
|
||||
<example>
|
||||
<title>function include</title>
|
||||
@@ -2337,6 +2336,86 @@ pass=foobar
|
||||
|
||||
{include file="footer.tpl" logo="http://my.domain.com/logo.gif"}
|
||||
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
<sect2 id="builtin.function.include.php">
|
||||
<title>include_php</title>
|
||||
<informaltable frame=all>
|
||||
<tgroup cols=3>
|
||||
<colspec colname=param>
|
||||
<colspec colname=type>
|
||||
<colspec colname=required>
|
||||
<colspec colname=default>
|
||||
<colspec colname=desc>
|
||||
<thead>
|
||||
<row>
|
||||
<entry>Attribute Name</entry>
|
||||
<entry>Type</entry>
|
||||
<entry>Required</entry>
|
||||
<entry>Default</entry>
|
||||
<entry>Description</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry>file</entry>
|
||||
<entry>string</entry>
|
||||
<entry>Yes</entry>
|
||||
<entry><emphasis>n/a</emphasis></entry>
|
||||
<entry>The name of the php file to include</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</informaltable>
|
||||
<para>
|
||||
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.
|
||||
</para>
|
||||
<para>
|
||||
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.
|
||||
</para>
|
||||
<para>
|
||||
include_php was added to Smarty 1.5.0.
|
||||
</para>
|
||||
<example>
|
||||
<title>function include_php</title>
|
||||
<programlisting>
|
||||
|
||||
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}
|
||||
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Project: Smarty: the PHP compiling template engine
|
||||
* File: Smarty.class.php
|
||||
|
@@ -435,13 +435,24 @@ class Smarty_Compiler extends Smarty {
|
||||
$arg_value = $arg_value ? 'true' : 'false';
|
||||
$arg_list[] = "'$arg_name' => $arg_value";
|
||||
}
|
||||
|
||||
return "<?php " .
|
||||
if (!empty($attrs['assign'])) {
|
||||
$return = "<?php ob_start();\n";
|
||||
$return .=
|
||||
"\$_smarty_tpl_vars = \$this->_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 = "<?php " .
|
||||
"\$_smarty_tpl_vars = \$this->_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;
|
||||
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_include_php_tag
|
||||
@@ -456,8 +467,8 @@ class Smarty_Compiler extends Smarty {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->security) {
|
||||
$this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name);
|
||||
if ($this->security) {
|
||||
if( $resource_type != 'file' || !@is_file($resource_name)) {
|
||||
$this->_syntax_error("include_php: $resource_type: $resource_name is not readable");
|
||||
return false;
|
||||
@@ -468,7 +479,14 @@ class Smarty_Compiler extends Smarty {
|
||||
}
|
||||
}
|
||||
|
||||
return "<?php include('".$resource_name."'); ?>";
|
||||
if (!empty($attrs['assign'])) {
|
||||
$return = "<?php ob_start();\n";
|
||||
$return .= "include('".$resource_name."');\n";
|
||||
$return .= "\$this->assign('".$this->_dequote($attrs['assign'])."',ob_get_contents()); ob_end_clean();\n?>";
|
||||
} else {
|
||||
$return = "<?php include('".$resource_name."'); ?>";
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user