update docs with new changes

This commit is contained in:
mohrt
2001-04-26 17:27:40 +00:00
parent a05dae2071
commit ea5d42daf4
9 changed files with 326 additions and 105 deletions

2
FAQ
View File

@@ -139,4 +139,4 @@ Q: My ISP did not setup the PEAR repository, nor will they set it up. How do I
A: The easiest thing to do is grab all of PEAR and install it locally for your A: The easiest thing to do is grab all of PEAR and install it locally for your
own use. There's nothing that says PEAR must be installed in its default own use. There's nothing that says PEAR must be installed in its default
directory. There won't be a version of Smarty that runs without PEAR, as it directory. There won't be a version of Smarty that runs without PEAR, as it
quite dependant on it, especially with database support. quite dependant on it.

5
NEWS
View File

@@ -1,5 +1,8 @@
Version 1.4.0 Version 1.4.0
------------- -------------
- added "componentized templates" tip into documentation (Monte)
- added {php}{/php} tags for embedding php code into templates (Monte)
- changed default value of $show_info_header to false (Monte)
- implemented '->' syntax for accessing properties of objects passed to the - implemented '->' syntax for accessing properties of objects passed to the
template. (Andrei) template. (Andrei)
- allowed custom functions to receive Smarty object as the second - allowed custom functions to receive Smarty object as the second
@@ -25,7 +28,7 @@ Version 1.4.0
- changed Smarty to compile templates at runtime, allowing for arbitrary - changed Smarty to compile templates at runtime, allowing for arbitrary
template resources. (Monte) template resources. (Monte)
- added fix for LOCK_EX under Windows and changed a couple of file - added fix for LOCK_EX under Windows and changed a couple of file
permissions for security. (Monte) permissions for security. (Monte, Fernando Nunes)
- allow arbitrary date strings to date_format, html_select_date and - allow arbitrary date strings to date_format, html_select_date and
html_select_time (Monte) html_select_time (Monte)

View File

@@ -62,7 +62,7 @@ drwxrwxrwx 2 user group 512 Jan 18 14:18 templates_c/
Now we need to create two files, index.php and templates/index.tpl. index.php Now we need to create two files, index.php and templates/index.tpl. index.php
is the file that we will be calling from our web browser. index.tpl is the file is the file that we will be calling from our web browser. index.tpl is the file
that Smarty will use as it's template file. (.tpl files are never called that Smarty will use as its template file. (.tpl files are never called
directly from the browser, only Smarty calls them.) directly from the browser, only Smarty calls them.)
@@ -178,7 +178,7 @@ $smarty->display("index.tpl");
--------- templates/header.tpl -------- --------- templates/header.tpl --------
<HTML> <HTML>
<TITLE>{$title}</TITLE> <TITLE>{$title|default:"no title"}</TITLE>
<BODY> <BODY>
--------- templates/footer.tpl -------- --------- templates/footer.tpl --------
@@ -191,7 +191,7 @@ You can pass as many variables as you want. The included file inherits all the
current template vars, plus any that are passed to it. The passed variables are current template vars, plus any that are passed to it. The passed variables are
only available within the scope of the included file. Also notice the way the only available within the scope of the included file. Also notice the way the
$title variable is printed to the template. It uses a variable modifier called $title variable is printed to the template. It uses a variable modifier called
"default". Printing {$title} means that if the value of "default". Printing {$title|default:"no title"} means that if the value of
$title is empty, the text "no title" will be printed instead of nothing. $title is empty, the text "no title" will be printed instead of nothing.
IF/ELSEIF/ELSE IF/ELSEIF/ELSE

View File

@@ -41,7 +41,12 @@ problems when displaying content other than HTML, so now you must explicitly
set this flag to true to show the header information (or change the default in set this flag to true to show the header information (or change the default in
your copy of Smarty.) your copy of Smarty.)
CHANGES/ENHANCEMENTS Documentation is written in docbook format. I updated the docbook -> HTML
generating software & style-sheets, and consequently the examples are no longer
in a different background color. If anyone wants to contribute a better
stylesheet or help with documentation, drop me a line. <monte@ispi.net>
CHANGES/ENHANCEMENTS/UPDATES
date_format, html_select_date and html_select_time used to require a unix date_format, html_select_date and html_select_time used to require a unix
timestamp as the format of the date passed into the template. Smarty is now a timestamp as the format of the date passed into the template. Smarty is now a
@@ -64,9 +69,20 @@ The run-time custom functions are now passed Smarty object as the second
parameter. This can be used, for example, to assign or clear template variables parameter. This can be used, for example, to assign or clear template variables
from inside the custom function. from inside the custom function.
clear_compile_dir() was added for clearing out compiled versions of your
templates. Not something normally needed, but you may have a need for this if
you have $compile_check set to false and you periodically update templates via
some automated process. As of 1.4.0, uncompiled templates _always_ get
compiled, regardless of $compile_check setting, although they won't be checked
for recompile if $compile_check is set to false.
You can now refer to properties of objects assigned from PHP by using the '->' You can now refer to properties of objects assigned from PHP by using the '->'
symbol and specifying the property name after it, e.g. $foo->bar. symbol and specifying the property name after it, e.g. $foo->bar.
{php}{/php} tags were added to embed php into the templates. Not normally
needed, but some circumstances may call for it. Check out the "componentized
templates" tip in the documentation for an example of needed usage.
UPGRADE NOTES UPGRADE NOTES
The format of the files created in the $compile_dir are now a bit different. The format of the files created in the $compile_dir are now a bit different.

View File

@@ -136,7 +136,7 @@ class Smarty
var $compiler_class = 'Smarty_Compiler'; // the compiler class used by var $compiler_class = 'Smarty_Compiler'; // the compiler class used by
// Smarty to compile templates // Smarty to compile templates
var $resource_funcs = array(); // what functions resource handlers are mapped to var $resource_funcs = array(); // what functions resource handlers are mapped to
var $filter_funcs = array(); // what functions templates are filtered through var $prefilter_funcs = array(); // what functions templates are prefiltered through
// before being compiled // before being compiled
/**************************************************************************/ /**************************************************************************/
@@ -304,28 +304,28 @@ class Smarty
} }
/*======================================================================*\ /*======================================================================*\
Function: register_filter Function: register_prefilter
Purpose: Registers a filter function to apply Purpose: Registers a prefilter function to apply
to a template before compiling to a template before compiling
\*======================================================================*/ \*======================================================================*/
function register_filter($function_name) function register_prefilter($function_name)
{ {
$this->filter_funcs[] = $function_name; $this->prefilter_funcs[] = $function_name;
} }
/*======================================================================*\ /*======================================================================*\
Function: unregister_filter Function: unregister_prefilter
Purpose: Unregisters a filter Purpose: Unregisters a prefilter
\*======================================================================*/ \*======================================================================*/
function unregister_filter($function_name) function unregister_prefilter($function_name)
{ {
$tmp_array = array(); $tmp_array = array();
foreach($this->filter_funcs as $curr_func) { foreach($this->prefilter_funcs as $curr_func) {
if($curr_func != $function_name) { if($curr_func != $function_name) {
$tmp_array[] = $curr_func; $tmp_array[] = $curr_func;
} }
} }
$this->filter_funcs = $tmp_array; $this->prefilter_funcs = $tmp_array;
} }
/*======================================================================*\ /*======================================================================*\
@@ -407,6 +407,37 @@ class Smarty
$this->_tpl_vars = array(); $this->_tpl_vars = array();
} }
/*======================================================================*\
Function: clear_compile_dir()
Purpose: clears compiled version of specified template resource,
or all compiled template files if one is not specified.
This function is for advanced use only, not normally needed.
\*======================================================================*/
function clear_compile_dir($tpl_file=null)
{
if (!is_dir($this->compile_dir))
return false;
if($tpl_file) {
// remove compiled template file if it exists
$tpl_file = urlencode($tpl_file).'.php';
if(file_exists($this->compile_dir.'/'.$tpl_file)) {
unlink($this->compile_dir.'/'.$tpl_file);
}
} else {
// remove everything in $compile_dir
$dir_handle = opendir($this->compile_dir);
while ($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_dir == '..' ||
!is_file($this->compile_dir.'/'.$curr_file))
{ continue; }
unlink($this->compile_dir.'/'.$curr_file);
}
closedir($dir_handle);
}
return true;
}
/*======================================================================*\ /*======================================================================*\
Function: get_template_vars Function: get_template_vars
@@ -649,7 +680,7 @@ class Smarty
$smarty_compiler->custom_funcs = $this->custom_funcs; $smarty_compiler->custom_funcs = $this->custom_funcs;
$smarty_compiler->custom_mods = $this->custom_mods; $smarty_compiler->custom_mods = $this->custom_mods;
$smarty_compiler->version = $this->version; $smarty_compiler->version = $this->version;
$smarty_compiler->filter_funcs = $this->filter_funcs; $smarty_compiler->prefilter_funcs = $this->prefilter_funcs;
$smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))

View File

@@ -43,6 +43,7 @@ class Smarty_Compiler extends Smarty {
// internal vars // internal vars
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
var $_literal_blocks = array(); // keeps literal template blocks var $_literal_blocks = array(); // keeps literal template blocks
var $_php_blocks = array(); // keeps php code blocks
var $_current_file = null; // the current template being compiled var $_current_file = null; // the current template being compiled
var $_current_line_no = 1; // line number for error messages var $_current_line_no = 1; // line number for error messages
@@ -54,13 +55,13 @@ class Smarty_Compiler extends Smarty {
function _compile_file($tpl_file, $template_source, &$template_compiled) function _compile_file($tpl_file, $template_source, &$template_compiled)
{ {
// run template source through functions registered in filter_funcs // run template source through functions registered in prefilter_funcs
if(is_array($this->filter_funcs) && count($this->filter_funcs) > 0) { if(is_array($this->prefilter_funcs) && count($this->prefilter_funcs) > 0) {
foreach($this->filter_funcs as $curr_func) { foreach($this->prefilter_funcs as $curr_func) {
if(function_exists($curr_func)) { if(function_exists($curr_func)) {
$template_source = $curr_func($template_source); $template_source = $curr_func($template_source);
} else { } else {
$this->_trigger_error_msg("filter function $curr_func does not exist."); $this->_trigger_error_msg("prefilter function $curr_func does not exist.");
} }
} }
} }
@@ -74,8 +75,14 @@ class Smarty_Compiler extends Smarty {
preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source, $match); preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source, $match);
$this->_literal_blocks = $match[1]; $this->_literal_blocks = $match[1];
$template_source = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s",
$this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_source); $this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_source);
/* Pull out the php code blocks. */
preg_match_all("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s", $template_source, $match);
$this->_php_blocks = $match[1];
$template_source = preg_replace("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s",
$this->quote_replace($this->left_delimiter.'php'.$this->right_delimiter), $template_source);
/* Gather all template tags. */ /* Gather all template tags. */
preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_source, $match); preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_source, $match);
$template_tags = $match[1]; $template_tags = $match[1];
@@ -226,6 +233,11 @@ class Smarty_Compiler extends Smarty {
$this->_current_line_no += substr_count($literal_block, "\n"); $this->_current_line_no += substr_count($literal_block, "\n");
return "<?php echo '".str_replace("'","\'",$literal_block)."'; ?>\n"; return "<?php echo '".str_replace("'","\'",$literal_block)."'; ?>\n";
case 'php':
list (,$php_block) = each($this->_php_blocks);
$this->_current_line_no += substr_count($php_block, "\n");
return '<?php '.$php_block.' ?>';
case 'insert': case 'insert':
return $this->_compile_insert_tag($tag_args); return $this->_compile_insert_tag($tag_args);

250
docs.sgml
View File

@@ -134,16 +134,16 @@
<sect2 id="section.caching"> <sect2 id="section.caching">
<title>Caching</title> <title>Caching</title>
<para> <para>
Smarty can cache the output of your generated templates. By default Smarty can cache the output of your generated templates. By default
this is disabled. If you <link linkend="setting.caching">enable this is disabled. If you <link linkend="setting.caching">enable
caching</link>, Smarty will store a copy of the generated template caching</link>, Smarty will store a copy of the generated template
output, and use this until the copy <link output, and use this until the copy <link
linkend="setting.cache.lifetime">expires</link>, regenerating a new linkend="setting.cache.lifetime">expires</link>, regenerating a new
one. The default cache expire time can be configured from the one. The default cache expire time can be configured from the
class. The exception to the rule is the <link class. The exception to the rule is the <link
linkend="function.insert">insert</link> tag. Anything generated by linkend="builtin.function.insert">insert</link> tag. Anything
the insert tag is not cached, but run dynamically on every generated by the insert tag is not cached, but run dynamically on
invocation, even within cached content. every invocation, even within cached content.
</para> </para>
<para> <para>
TECHNICAL NOTE: Any time you change a template, change values in TECHNICAL NOTE: Any time you change a template, change values in
@@ -289,33 +289,44 @@ chmod 700 cache
<sect2 id="setting.global.assign"> <sect2 id="setting.global.assign">
<title>$global_assign</title> <title>$global_assign</title>
<para> <para>
This is a list of variables that are always implicitly assigned This is a list of variables that are always implicitly assigned
to the template engine. This is handy for making global to the template engine. This is handy for making global
variables or server variables available to all templates variables or server variables available to all templates
without having to manually assign them. $SCRIPT_NAME is without having to manually assign them. $SCRIPT_NAME is
globally assigned by default. globally assigned by default.
</para>
</sect2>
<sect2 id="setting.undefined">
<title>$undefined</title>
<para>
This sets the value of $undefined for Smarty, default is null.
Currently this is only used to set undefined variables in
$global_assign to a default value.
</para> </para>
</sect2> </sect2>
<sect2> <sect2>
<title>$compile_check</title> <title>$compile_check</title>
<para> <para>
Upon each invocation of the PHP application, Smarty tests to Upon each invocation of the PHP application, Smarty tests to
see if the current template has changed (later time stamp) see if the current template has changed (later time stamp)
since the last time it was compiled. If it has changed, it since the last time it was compiled. If it has changed, it
recompiles that template. By default this variable is set to recompiles that template. As of 1.4.0, if the template has not
true. Once an application is put into production and all been compiled, it will compile regardless of this setting. By
templates are initially compiled, the compile_check step is no default this variable is set to true. Once an application is
longer needed. Be sure to set $compile_check to "false" to put into production (templates won't be changing), the
improve performance! Note that if you change this to "false" compile_check step is no longer needed. Be sure to set
and a template file is changed, you will *not* see the change $compile_check to "false" to improve performance! Note that if
since the template will not get recompiled. See <link you change this to "false" and a template file is changed, you
linkend="setting.force.compile">$force_compile</link> will *not* see the change since the template will not get
recompiled. See <link
linkend="setting.force.compile">$force_compile</link> or <link
linkend="api.clear.compile.dir">clear_compile_dir</link>.
</para> </para>
</sect2> </sect2>
<sect2 id="setting.force.compile"> <sect2 id="setting.force.compile">
<title>$force_compile</title> <title>$force_compile</title>
<para> <para>
This forces Smarty to (re)compile each template on every This forces Smarty to (re)compile templates on every
invocation. This setting overrides $compile_check. By default invocation. This setting overrides $compile_check. By default
this is disabled. This is handy for development and debugging. this is disabled. This is handy for development and debugging.
It should never be used in a production environment. It should never be used in a production environment.
@@ -364,10 +375,10 @@ chmod 700 cache
<sect2 id="setting.insert.tag.check"> <sect2 id="setting.insert.tag.check">
<title>$insert_tag_check</title> <title>$insert_tag_check</title>
<para> <para>
If you have $caching enabled and you do not use the <link If you have $caching enabled and you do not use the <link
linkend="function.insert">insert</link> tag anywhere in your linkend="builtin.function.insert">insert</link> tag anywhere in
templates, set this to false. This saves the insert tag search, your templates, set this to false. This saves the insert tag
speeding up cached page fetches. search, speeding up cached page fetches.
</para> </para>
</sect2> </sect2>
<sect2 id="setting.tpl.file.ext"> <sect2 id="setting.tpl.file.ext">
@@ -400,9 +411,11 @@ chmod 700 cache
<sect2 id="setting.php.handling"> <sect2 id="setting.php.handling">
<title>$php_handling</title> <title>$php_handling</title>
<para> <para>
This tells Smarty how to handle PHP code embedded in the This tells Smarty how to handle PHP code embedded in the
tempalates. There are four possible settings, default being tempalates. There are four possible settings, default being
SMARTY_PHP_PASSTHRU. SMARTY_PHP_PASSTHRU. Note that this does NOT affect php code
within <link linkend="builtin.function.php">{php}{/php}</link>
tags in the template.
</para> </para>
<itemizedlist> <itemizedlist>
<listitem><para>SMARTY_PHP_PASSTHRU - Smarty echos tags as-is.</para></listitem> <listitem><para>SMARTY_PHP_PASSTHRU - Smarty echos tags as-is.</para></listitem>
@@ -647,6 +660,33 @@ $smarty->clear_cache("index.tpl","CACHEID");
$smarty->clear_all_cache(); $smarty->clear_all_cache();
</programlisting>
</example>
</sect2>
<sect2 id="api.clear.compile.dir">
<title>clear_compile_dir</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>clear_all_cache</function></funcdef>
<paramdef>string <parameter>tpl_file</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This clears the compiled version of the specified template
resource, or all compiled template files if one is not specified.
This function is for advanced use only, not normally needed.
</para>
<example>
<title>clear_compile_dir</title>
<programlisting>
// clear a specific template resource
$smarty->clear_compile_dir("index.tpl");
// clear entire compile directory
$smarty->clear_compile_dir();
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -801,45 +841,45 @@ $smarty->unregister_resource("db");
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2 id="api.register.filter"> <sect2 id="api.register.prefilter">
<title>register_filter</title> <title>register_prefilter</title>
<funcsynopsis> <funcsynopsis>
<funcprototype> <funcprototype>
<funcdef>void <function>register_filter</function></funcdef> <funcdef>void <function>register_prefilter</function></funcdef>
<paramdef>string <parameter>function_name</parameter></paramdef> <paramdef>string <parameter>function_name</parameter></paramdef>
</funcprototype> </funcprototype>
</funcsynopsis> </funcsynopsis>
<para> <para>
Use this to dynamically register filters to run templates Use this to dynamically register prefilters to run templates
through before they are compiled. See <link through before they are compiled. See <link
linkend="section.template.filters">template filters</link> for linkend="section.template.prefilters">template prefilters</link> for
more information on how to setup a filtering function. more information on how to setup a prefiltering function.
</para> </para>
<example> <example>
<title>register_filter</title> <title>register_prefilter</title>
<programlisting> <programlisting>
$smarty->register_filter("remove_dw_comments"); $smarty->register_prefilter("remove_dw_comments");
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2 id="api.unregister.filter"> <sect2 id="api.unregister.prefilter">
<title>unregister_filter</title> <title>unregister_prefilter</title>
<funcsynopsis> <funcsynopsis>
<funcprototype> <funcprototype>
<funcdef>void <function>unregister_filter</function></funcdef> <funcdef>void <function>unregister_prefilter</function></funcdef>
<paramdef>string <parameter>function_name</parameter></paramdef> <paramdef>string <parameter>function_name</parameter></paramdef>
</funcprototype> </funcprototype>
</funcsynopsis> </funcsynopsis>
<para> <para>
Use this to dynamically unregister a filter. Use this to dynamically unregister a prefilter.
</para> </para>
<example> <example>
<title>unregister_filter</title> <title>unregister_prefilter</title>
<programlisting> <programlisting>
$smarty->unregister_filter("imbed_asp_jokes"); $smarty->unregister_prefilter("imbed_asp_jokes");
</programlisting> </programlisting>
</example> </example>
@@ -1180,26 +1220,26 @@ $smarty->display("db:index.tpl");
</example> </example>
</sect2> </sect2>
</sect1> </sect1>
<sect1 id="section.template.filters"> <sect1 id="section.template.prefilters">
<title>Template Filters</title> <title>Template Preilters</title>
<para> <para>
Template filters are PHP functions that your templates are ran through Template prefilters are PHP functions that your templates are ran through
before they are compiled. This is good for preprocessing your templates before they are compiled. This is good for preprocessing your templates
to remove unwanted comments, keeping an eye on what people are putting to remove unwanted comments, keeping an eye on what people are putting
in their templates, etc. Filters are processed in the order they are in their templates, etc. Prefilters are processed in the order they are
<link linkend="api.register.filter">registered</link>. <link linkend="api.register.prefilter">registered</link>.
</para> </para>
<sect2> <sect2>
<title>Example of a Template Filter</title> <title>Example of a Template Prefilter</title>
<para> <para>
Create a function in your application that Smarty will use as a Create a function in your application that Smarty will use as a
template filter. Smarty will pass the template source code as the template prefilter. Smarty will pass the template source code as the
first argument, an expect the function to return the resulting first argument, an expect the function to return the resulting
template source code. template source code.
</para> </para>
<example> <example>
<title>using a template filter</title> <title>using a template prefilter</title>
<programlisting> <programlisting>
// put this in your application, or in Smarty.addons.php // put this in your application, or in Smarty.addons.php
@@ -1207,12 +1247,12 @@ function remove_dw_comments($tpl_source) {
return preg_replace("/&lt;!--#.*--&gt;/U","",$tpl_source); return preg_replace("/&lt;!--#.*--&gt;/U","",$tpl_source);
} }
// register the filter // register the prefilter
$smarty->register_filter("remove_dw_comments"); $smarty->register_prefilter("remove_dw_comments");
$smarty->display("index.tpl"); $smarty->display("index.tpl");
{* from within Smarty template *} {* from within Smarty template *}
&lt;!--# this line will get removed by the filter --&gt; &lt;!--# this line will get removed by the prefilter --&gt;
</programlisting> </programlisting>
</example> </example>
@@ -1617,7 +1657,7 @@ Intro = """This is a value that spans more
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="builtin.function.include">
<title>include</title> <title>include</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -1691,7 +1731,7 @@ Intro = """This is a value that spans more
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2 id="function.insert"> <sect2 id="builtin.function.insert">
<title>insert</title> <title>insert</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -1878,7 +1918,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="builtin.funciton.literal">
<title>literal</title> <title>literal</title>
<para> <para>
Literal tags allow a block of data to be taken literally, Literal tags allow a block of data to be taken literally,
@@ -1909,6 +1949,28 @@ OUTPUT:
&lt;/script&gt; &lt;/script&gt;
{/literal} {/literal}
</programlisting>
</example>
</sect2>
<sect2 id="builtin.function.php">
<title>php</title>
<para>
php tags allow php to be imbedded directly into the template. They
will not be escaped, regardless of the <link
linkend="setting.php.handling">$php_handling</link> setting. This
is for advanced users only, not normally needed. This was added to
1.4.0.
</para>
<example>
<title>php tags</title>
<programlisting>
{php}
// including a php script directly
// from the template.
include("/path/to/display_weather.php");
{/php}
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -2498,7 +2560,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="custom.functions.html.options">
<title>html_options</title> <title>html_options</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -2587,7 +2649,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="custom.functions.html.select.date">
<title>html_select_date</title> <title>html_select_date</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -2783,7 +2845,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="custom.functions.html.select.time">
<title>html_select_time</title> <title>html_select_time</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -3040,7 +3102,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2> <sect2 id="custom.functions.math">
<title>math</title> <title>math</title>
<informaltable frame=all> <informaltable frame=all>
<tgroup cols=3> <tgroup cols=3>
@@ -3975,6 +4037,10 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75
linkend="date.format">date_format</link> for full control over date linkend="date.format">date_format</link> for full control over date
formatting, and also makes it easy to compare dates if necessary. formatting, and also makes it easy to compare dates if necessary.
</para> </para>
<para>
NOTE: As of Smarty 1.4.0, you can pass dates to Smarty as unix
timestamps, mysql timestamps, or any date parsable by strtotime().
</para>
<example> <example>
<title>using date_format</title> <title>using date_format</title>
<programlisting> <programlisting>
@@ -4083,9 +4149,59 @@ Pretty easy isn't it?
</programlisting> </programlisting>
</example> </example>
</sect1> </sect1>
<sect1 id="tips.componentized.templates">
<title>Componentized Templates</title>
<para>
This tip is a bit of a hack, but still a neat idea. Use at your own
risk. ;-)
</para>
<para>
Traditionally, programming templates into your applications goes as
follows: First, you accumulate your variables within your PHP
application, (maybe with database queries.) Then, you instantiate your
Smarty object, assign the variables and display the template. So lets
say for example we have a stock ticker on our template. We would
collect the stock data in our application, then assign these variables
in the template and display it. Now wouldn't it be nice if you could
add this stock ticker to any application by merely including the
template, and not worry about fetching the data up front?
</para>
<para>
As of Smarty 1.4.0, you can embed php into your templates with the
{php}{/php} tags. With this, you can setup self contained templates
with their own data structures for assigning their own variables. With
the logic embedded like this, you can keep the template & logic
together. This way no matter where the template source is coming from,
it is always together as one component.
</para>
<example>
<title>componentized template</title>
<programlisting>
{* Smarty *}
{php}
// setup our function for fetching stock data
function fetch_ticker($symbol,&$ticker_name,&$ticker_price) {
// put logic here that fetches $ticker_name
// and $ticker_price from some resource
}
// call the function
fetch_ticker("YHOO",$ticker_name,$ticker_price);
// assign template variables
$this->assign("ticker_name",$ticker_name);
$this->assign("ticker_price",$ticker_price);
{/php}
Stock Name: {$ticker_name} Stock Price: {$ticker_price}
</programlisting>
</example>
</sect1>
</chapter> </chapter>
<chapter id="resources"> <chapter id="resources">
<title>Resources</title> <title>Resources</title>
@@ -4093,7 +4209,7 @@ Pretty easy isn't it?
Smarty's homepage is located at http://www.phpinsider.com/php/code/Smarty/. Smarty's homepage is located at http://www.phpinsider.com/php/code/Smarty/.
You can join the mailing list by sending an e-mail to You can join the mailing list by sending an e-mail to
subscribe-smarty@lists.ispi.net. An archive of the mailing list can be subscribe-smarty@lists.ispi.net. An archive of the mailing list can be
viewed at http://marc.theaimsgroup.com/ under www/smarty. viewed at http://marc.theaimsgroup.com/?l=smarty&amp;r=1&amp;w=2
</para> </para>
</chapter> </chapter>
<chapter id="bugs"> <chapter id="bugs">

View File

@@ -136,7 +136,7 @@ class Smarty
var $compiler_class = 'Smarty_Compiler'; // the compiler class used by var $compiler_class = 'Smarty_Compiler'; // the compiler class used by
// Smarty to compile templates // Smarty to compile templates
var $resource_funcs = array(); // what functions resource handlers are mapped to var $resource_funcs = array(); // what functions resource handlers are mapped to
var $filter_funcs = array(); // what functions templates are filtered through var $prefilter_funcs = array(); // what functions templates are prefiltered through
// before being compiled // before being compiled
/**************************************************************************/ /**************************************************************************/
@@ -304,28 +304,28 @@ class Smarty
} }
/*======================================================================*\ /*======================================================================*\
Function: register_filter Function: register_prefilter
Purpose: Registers a filter function to apply Purpose: Registers a prefilter function to apply
to a template before compiling to a template before compiling
\*======================================================================*/ \*======================================================================*/
function register_filter($function_name) function register_prefilter($function_name)
{ {
$this->filter_funcs[] = $function_name; $this->prefilter_funcs[] = $function_name;
} }
/*======================================================================*\ /*======================================================================*\
Function: unregister_filter Function: unregister_prefilter
Purpose: Unregisters a filter Purpose: Unregisters a prefilter
\*======================================================================*/ \*======================================================================*/
function unregister_filter($function_name) function unregister_prefilter($function_name)
{ {
$tmp_array = array(); $tmp_array = array();
foreach($this->filter_funcs as $curr_func) { foreach($this->prefilter_funcs as $curr_func) {
if($curr_func != $function_name) { if($curr_func != $function_name) {
$tmp_array[] = $curr_func; $tmp_array[] = $curr_func;
} }
} }
$this->filter_funcs = $tmp_array; $this->prefilter_funcs = $tmp_array;
} }
/*======================================================================*\ /*======================================================================*\
@@ -407,6 +407,37 @@ class Smarty
$this->_tpl_vars = array(); $this->_tpl_vars = array();
} }
/*======================================================================*\
Function: clear_compile_dir()
Purpose: clears compiled version of specified template resource,
or all compiled template files if one is not specified.
This function is for advanced use only, not normally needed.
\*======================================================================*/
function clear_compile_dir($tpl_file=null)
{
if (!is_dir($this->compile_dir))
return false;
if($tpl_file) {
// remove compiled template file if it exists
$tpl_file = urlencode($tpl_file).'.php';
if(file_exists($this->compile_dir.'/'.$tpl_file)) {
unlink($this->compile_dir.'/'.$tpl_file);
}
} else {
// remove everything in $compile_dir
$dir_handle = opendir($this->compile_dir);
while ($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_dir == '..' ||
!is_file($this->compile_dir.'/'.$curr_file))
{ continue; }
unlink($this->compile_dir.'/'.$curr_file);
}
closedir($dir_handle);
}
return true;
}
/*======================================================================*\ /*======================================================================*\
Function: get_template_vars Function: get_template_vars
@@ -649,7 +680,7 @@ class Smarty
$smarty_compiler->custom_funcs = $this->custom_funcs; $smarty_compiler->custom_funcs = $this->custom_funcs;
$smarty_compiler->custom_mods = $this->custom_mods; $smarty_compiler->custom_mods = $this->custom_mods;
$smarty_compiler->version = $this->version; $smarty_compiler->version = $this->version;
$smarty_compiler->filter_funcs = $this->filter_funcs; $smarty_compiler->prefilter_funcs = $this->prefilter_funcs;
$smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))

View File

@@ -43,6 +43,7 @@ class Smarty_Compiler extends Smarty {
// internal vars // internal vars
var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part var $_sectionelse_stack = array(); // keeps track of whether section had 'else' part
var $_literal_blocks = array(); // keeps literal template blocks var $_literal_blocks = array(); // keeps literal template blocks
var $_php_blocks = array(); // keeps php code blocks
var $_current_file = null; // the current template being compiled var $_current_file = null; // the current template being compiled
var $_current_line_no = 1; // line number for error messages var $_current_line_no = 1; // line number for error messages
@@ -54,13 +55,13 @@ class Smarty_Compiler extends Smarty {
function _compile_file($tpl_file, $template_source, &$template_compiled) function _compile_file($tpl_file, $template_source, &$template_compiled)
{ {
// run template source through functions registered in filter_funcs // run template source through functions registered in prefilter_funcs
if(is_array($this->filter_funcs) && count($this->filter_funcs) > 0) { if(is_array($this->prefilter_funcs) && count($this->prefilter_funcs) > 0) {
foreach($this->filter_funcs as $curr_func) { foreach($this->prefilter_funcs as $curr_func) {
if(function_exists($curr_func)) { if(function_exists($curr_func)) {
$template_source = $curr_func($template_source); $template_source = $curr_func($template_source);
} else { } else {
$this->_trigger_error_msg("filter function $curr_func does not exist."); $this->_trigger_error_msg("prefilter function $curr_func does not exist.");
} }
} }
} }
@@ -74,8 +75,14 @@ class Smarty_Compiler extends Smarty {
preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source, $match); preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source, $match);
$this->_literal_blocks = $match[1]; $this->_literal_blocks = $match[1];
$template_source = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_source = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s",
$this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_source); $this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_source);
/* Pull out the php code blocks. */
preg_match_all("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s", $template_source, $match);
$this->_php_blocks = $match[1];
$template_source = preg_replace("!{$ldq}php{$rdq}(.*?){$ldq}/php{$rdq}!s",
$this->quote_replace($this->left_delimiter.'php'.$this->right_delimiter), $template_source);
/* Gather all template tags. */ /* Gather all template tags. */
preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_source, $match); preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_source, $match);
$template_tags = $match[1]; $template_tags = $match[1];
@@ -226,6 +233,11 @@ class Smarty_Compiler extends Smarty {
$this->_current_line_no += substr_count($literal_block, "\n"); $this->_current_line_no += substr_count($literal_block, "\n");
return "<?php echo '".str_replace("'","\'",$literal_block)."'; ?>\n"; return "<?php echo '".str_replace("'","\'",$literal_block)."'; ?>\n";
case 'php':
list (,$php_block) = each($this->_php_blocks);
$this->_current_line_no += substr_count($php_block, "\n");
return '<?php '.$php_block.' ?>';
case 'insert': case 'insert':
return $this->_compile_insert_tag($tag_args); return $this->_compile_insert_tag($tag_args);