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:
9
TESTIMONIALS
Normal file
9
TESTIMONIALS
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
"I just got my first paying contract for a local small business and in my
|
||||||
|
program I'm using Smarty. Smarty is an absolute joy to work with -
|
||||||
|
at first, I didn't like the idea of populating variables/arrays and then
|
||||||
|
assigning those directly (since I'd been using FastTemplates for so long),
|
||||||
|
but after using this system for a while, I don't think I can ever go back
|
||||||
|
(I even switched my Perl template engine to HTML-Template-2.4 which
|
||||||
|
operates on the same concepts). Anyway, thanks for saving me time and
|
||||||
|
headache!"
|
||||||
|
-- Brian E. Lozier
|
@@ -3,7 +3,7 @@
|
|||||||
<chapter id="troubleshooting">
|
<chapter id="troubleshooting">
|
||||||
<title>Troubleshooting</title>
|
<title>Troubleshooting</title>
|
||||||
<para></para>
|
<para></para>
|
||||||
<sect1>
|
<sect1 id="smarty.php.errors">
|
||||||
<title>Smarty/PHP errors</title>
|
<title>Smarty/PHP errors</title>
|
||||||
<para>
|
<para>
|
||||||
Smarty can catch many errors such as missing tag attributes
|
Smarty can catch many errors such as missing tag attributes
|
||||||
@@ -55,7 +55,7 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75
|
|||||||
<title>Tips & Tricks</title>
|
<title>Tips & Tricks</title>
|
||||||
<para>
|
<para>
|
||||||
</para>
|
</para>
|
||||||
<sect1>
|
<sect1 id="tips.blank.var.handling">
|
||||||
<title>Blank Variable Handling</title>
|
<title>Blank Variable Handling</title>
|
||||||
<para>
|
<para>
|
||||||
There may be times when you want to print a default value for an empty
|
There may be times when you want to print a default value for an empty
|
||||||
@@ -82,7 +82,8 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75
|
|||||||
{$title|default:"&nbsp;"}</programlisting>
|
{$title|default:"&nbsp;"}</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
<sect1>
|
|
||||||
|
<sect1 id="tips.default.var.handling">
|
||||||
<title>Default Variable Handling</title>
|
<title>Default Variable Handling</title>
|
||||||
<para>
|
<para>
|
||||||
If a variable is used frequently throughout your templates, applying
|
If a variable is used frequently throughout your templates, applying
|
||||||
@@ -100,7 +101,7 @@ Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75
|
|||||||
{$title}</programlisting>
|
{$title}</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
<sect1>
|
<sect1 id="tips.passing.vars">
|
||||||
<title>Passing variable title to header template</title>
|
<title>Passing variable title to header template</title>
|
||||||
<para>
|
<para>
|
||||||
When the majority of your templates use the same headers and footers, it
|
When the majority of your templates use the same headers and footers, it
|
||||||
@@ -154,7 +155,7 @@ footer.tpl
|
|||||||
<emphasis>default</emphasis> variable modifier.
|
<emphasis>default</emphasis> variable modifier.
|
||||||
</para>
|
</para>
|
||||||
</sect1>
|
</sect1>
|
||||||
<sect1>
|
<sect1 id="tips.dates">
|
||||||
<title>Dates</title>
|
<title>Dates</title>
|
||||||
<para>
|
<para>
|
||||||
As a rule of thumb, always pass dates to Smarty as timestamps.
|
As a rule of thumb, always pass dates to Smarty as timestamps.
|
||||||
@@ -213,7 +214,7 @@ function makeTimeStamp($year="",$month="",$day="")
|
|||||||
}</programlisting>
|
}</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
<sect1>
|
<sect1 id="tips.wap">
|
||||||
<title>WAP/WML</title>
|
<title>WAP/WML</title>
|
||||||
<para>
|
<para>
|
||||||
WAP/WML templates require a php Content-Type header to be passed along
|
WAP/WML templates require a php Content-Type header to be passed along
|
||||||
@@ -353,7 +354,7 @@ index.tpl
|
|||||||
Stock Name: {$ticker_name} Stock Price: {$ticker_price}</programlisting>
|
Stock Name: {$ticker_name} Stock Price: {$ticker_price}</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
<sect1>
|
<sect1 id="tips.obfuscating.email">
|
||||||
<title>Obfuscating E-mail Addresses</title>
|
<title>Obfuscating E-mail Addresses</title>
|
||||||
<para>
|
<para>
|
||||||
Do you ever wonder how your E-mail address gets on so many spam mailing
|
Do you ever wonder how your E-mail address gets on so many spam mailing
|
||||||
@@ -388,9 +389,8 @@ href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&a
|
|||||||
<note>
|
<note>
|
||||||
<title>Technical Note</title>
|
<title>Technical Note</title>
|
||||||
<para>
|
<para>
|
||||||
This method isn't 100% foolproof. Although highly unlikely,
|
This method isn't 100% foolproof. Although highly unlikely, a spammer
|
||||||
a spammer could conceivably write his e-mail collecter to decode these
|
could conceivably write his e-mail collector to decode these values.
|
||||||
values.
|
|
||||||
</para>
|
</para>
|
||||||
</note>
|
</note>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
<part>
|
<part id="smarty.for.designers">
|
||||||
<title>Smarty For Template Designers</title>
|
<title>Smarty For Template Designers</title>
|
||||||
<chapter id="language">
|
<chapter id="language">
|
||||||
<title>Template Language</title>
|
<title>Template Language</title>
|
||||||
@@ -141,7 +141,7 @@
|
|||||||
<body bgcolr="{#bgcolor#}"></programlisting>
|
<body bgcolr="{#bgcolor#}"></programlisting>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<sect2>
|
<sect2 id="language.assigned.variables">
|
||||||
<title>Variables assigned from PHP</title>
|
<title>Variables assigned from PHP</title>
|
||||||
<para>
|
<para>
|
||||||
Variables that are assigned from PHP are referenced by preceding
|
Variables that are assigned from PHP are referenced by preceding
|
||||||
@@ -162,7 +162,7 @@ Hello Doug, glad to see you could make it.
|
|||||||
Your last login was on January 11th, 2001.</programlisting>
|
Your last login was on January 11th, 2001.</programlisting>
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
<sect3>
|
<sect3 id="language.variables.assoc.arrays">
|
||||||
<title>Associative arrays</title>
|
<title>Associative arrays</title>
|
||||||
<para>
|
<para>
|
||||||
You can also reference associative array variables that are
|
You can also reference associative array variables that are
|
||||||
@@ -170,7 +170,7 @@ Your last login was on January 11th, 2001.</programlisting>
|
|||||||
symbol.
|
symbol.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
<title>displaying assigned associative array variables</title>
|
<title>accessing associative array variables</title>
|
||||||
<programlisting>
|
<programlisting>
|
||||||
{$Contacts.fax}<br>
|
{$Contacts.fax}<br>
|
||||||
{$Contacts.email}<br>
|
{$Contacts.email}<br>
|
||||||
@@ -187,14 +187,14 @@ zaphod@slartibartfast.com<br>
|
|||||||
</programlisting>
|
</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
<sect3>
|
<sect3 id="language.variables.array.indexes">
|
||||||
<title>Array indexes</title>
|
<title>Array indexes</title>
|
||||||
<para>
|
<para>
|
||||||
You can reference arrays by their index, much like native PHP
|
You can reference arrays by their index, much like native PHP
|
||||||
syntax.
|
syntax.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
<title>displaying arrays by index</title>
|
<title>accessing arrays by index</title>
|
||||||
<programlisting>
|
<programlisting>
|
||||||
{$Contacts[0]}<br>
|
{$Contacts[0]}<br>
|
||||||
{$Contacts[1]}<br>
|
{$Contacts[1]}<br>
|
||||||
@@ -203,14 +203,14 @@ zaphod@slartibartfast.com<br>
|
|||||||
{$Contacts[0][1]}<br></programlisting>
|
{$Contacts[0][1]}<br></programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
<sect3>
|
<sect3 id="language.variables.objects">
|
||||||
<title>Objects</title>
|
<title>Objects</title>
|
||||||
<para>
|
<para>
|
||||||
Properties of objects assigned from PHP can be referenced
|
Properties of objects assigned from PHP can be referenced
|
||||||
by specifying the property name after the '->' symbol.
|
by specifying the property name after the '->' symbol.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
<title>displaying object properties</title>
|
<title>accessing object properties</title>
|
||||||
<programlisting>
|
<programlisting>
|
||||||
name: {$person->name}<br>
|
name: {$person->name}<br>
|
||||||
email: {$person->email}<br>
|
email: {$person->email}<br>
|
||||||
@@ -221,19 +221,17 @@ name: Zaphod Beeblebrox<br>
|
|||||||
email: zaphod@slartibartfast.com<br></programlisting>
|
email: zaphod@slartibartfast.com<br></programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
|
||||||
<sect2>
|
<sect2 id="language.config.variables">
|
||||||
<title>Variables loaded from config files</title>
|
<title>Variables loaded from config files</title>
|
||||||
<para>
|
<para>
|
||||||
Variables that are passed in from config files are displayed by enclosing
|
Variables that are loaded from the config files are referenced by enclosing
|
||||||
them with hash marks (#) and enclosing the variable in delimiters
|
them within hash marks (#).
|
||||||
like so: {#varname#}
|
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
|
|
||||||
|
<title>config variables</title>
|
||||||
<title>displaying config variables</title>
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
<html>
|
<html>
|
||||||
<title>{#pageTitle#}</title>
|
<title>{#pageTitle#}</title>
|
||||||
@@ -249,43 +247,31 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
|||||||
</html></programlisting>
|
</html></programlisting>
|
||||||
</example>
|
</example>
|
||||||
<para>
|
<para>
|
||||||
Config file variables cannot be displayed until
|
Config file variables cannot be used until
|
||||||
after they are loaded in from a config file. This procedure is
|
after they are loaded in from a config file. This procedure is
|
||||||
explained later in this document under
|
explained later in this document under
|
||||||
<link linkend="builtin.functions.configload">config_load</link>.
|
<command>config_load</command>.
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
<sect2>
|
|
||||||
<title>Variables internal to template</title>
|
|
||||||
<para>
|
|
||||||
Variables that are internal to the templates are displayed by using
|
|
||||||
the special variable {$smarty}. For instance, you can reference the
|
|
||||||
current date/time with {$smarty.now}, or <link
|
|
||||||
linkend="builtin.functions.section">section loops</link> have
|
|
||||||
properties that are internal variables.
|
|
||||||
</para>
|
|
||||||
</sect2>
|
|
||||||
<sect2 id="builtin.variable.smarty">
|
|
||||||
<title>{$smarty} reserved variable</title>
|
|
||||||
<para>
|
|
||||||
The reserved {$smarty} variable can be used to access several
|
|
||||||
special template variables. The full list of them follows.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
The special variable {$smarty} was added to Smarty 1.4.4.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<sect3>
|
<sect2 id="language.variables.smarty">
|
||||||
<title>Request variables</title>
|
<title>{$smarty} reserved variable</title>
|
||||||
<para>
|
<para>
|
||||||
The request variables such as get, post, cookies, server,
|
The reserved {$smarty} variable can be used to access several
|
||||||
environment, and session variables can be accessed as demonstrated
|
special template variables. The full list of them follows.
|
||||||
in the examples below:
|
</para>
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>displaying request variables</title>
|
<sect3 id="language.variables.smarty.request">
|
||||||
<programlisting>
|
<title>Request variables</title>
|
||||||
|
<para>
|
||||||
|
The request variables such as get, post, cookies, server,
|
||||||
|
environment, and session variables can be accessed as demonstrated
|
||||||
|
in the examples below:
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
|
||||||
|
<title>displaying request variables</title>
|
||||||
|
<programlisting>
|
||||||
{* display the variable "page" given in the URL, or from a form using the GET method *}
|
{* display the variable "page" given in the URL, or from a form using the GET method *}
|
||||||
{$smarty.get.page}
|
{$smarty.get.page}
|
||||||
|
|
||||||
@@ -309,7 +295,7 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
|||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
|
|
||||||
<sect3>
|
<sect3 id="language.variables.smarty.now">
|
||||||
<title>Current timestamp</title>
|
<title>Current timestamp</title>
|
||||||
<para>
|
<para>
|
||||||
The current timestamp can be accessed with {$smarty.now}. The
|
The current timestamp can be accessed with {$smarty.now}. The
|
||||||
@@ -326,7 +312,7 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
|||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
|
|
||||||
<sect3>
|
<sect3 id="language.variables.smarty.capture">
|
||||||
<title>Output capture buffer</title>
|
<title>Output capture buffer</title>
|
||||||
<para>
|
<para>
|
||||||
The output captured via {capture}..{/capture} construct can be
|
The output captured via {capture}..{/capture} construct can be
|
||||||
@@ -335,7 +321,7 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
|||||||
</para>
|
</para>
|
||||||
</sect3>
|
</sect3>
|
||||||
|
|
||||||
<sect3>
|
<sect3 id="language.variables.smarty.loops">
|
||||||
<title>Loop properties</title>
|
<title>Loop properties</title>
|
||||||
<para>
|
<para>
|
||||||
{$smarty} variable can be used to refer to 'section' and
|
{$smarty} variable can be used to refer to 'section' and
|
||||||
@@ -344,43 +330,46 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
|||||||
<link linkend="builtin.functions.foreach">foreach</link>.
|
<link linkend="builtin.functions.foreach">foreach</link>.
|
||||||
</para>
|
</para>
|
||||||
</sect3>
|
</sect3>
|
||||||
|
|
||||||
</sect2>
|
</sect2>
|
||||||
|
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="variable.modifiers">
|
<sect1 id="language.modifiers">
|
||||||
<title>Variable Modifiers</title>
|
<title>Variable Modifiers</title>
|
||||||
<para>
|
<para>
|
||||||
Variable modifiers are a bit different than <link
|
Variable modifiers can be applied to any variable to alter its contents. To
|
||||||
linkend="bundled.plugins.commands">custom functions</link>. Variable modifiers
|
apply a modifier, specify the variable followed by the <literal>|</literal>
|
||||||
alter variable contents before they are displayed to the template. All
|
(pipe) and the modifier name. A modifier may accept additional parameters
|
||||||
modifiers will get the value of the variable as the first argument, and
|
that affect its behavior. These parameters follow the modifer name and are
|
||||||
must return a single value. Modifier parameters are separated by
|
separated by <literal>:</literal> (colon).
|
||||||
colons. Any additional parameters passed to a modifier are passed as-is
|
</para>
|
||||||
positionally, much like calling a PHP function. You can also use native
|
<example>
|
||||||
PHP functions as modifiers, but only if they expect the correct
|
<title>modifier example</title>
|
||||||
arguments. If they do not, you can always write a wrapper function in
|
<programlisting>
|
||||||
Smarty to get what you want. You can chain as many modifiers together
|
{* Uppercase the title *}
|
||||||
on a variable as you like, separating each with a vertical pipe "|".
|
<h2>{$title|upper}</h2>
|
||||||
</para>
|
|
||||||
<para>
|
{* Truncate the topic to 40 characters use ... at the end *}
|
||||||
NOTE: if you apply a modifier to an array instead of a single value
|
Topic: {$topic|truncate:40:"..."}</programlisting>
|
||||||
variable, the modifier will be applied to every value in that array. If
|
</example>
|
||||||
you really want the entire array passed to the modifier, you must
|
<para>
|
||||||
prepend it with an "@" sign like so: {$articleTitle|@count} (this will
|
If you apply a modifier to an array variable instead of a single value variable,
|
||||||
print out the number of elements in the $articleTitle array.)
|
the modifier will be applied to every value in that array. If you really want
|
||||||
</para>
|
the modifier to work on an entire array as a value, you must prepend the
|
||||||
<sect2 id="language.builtin.modifiers">
|
modifier name with an <literal>@</literal> symbol like so:
|
||||||
<title>Built-in Modifiers</title>
|
<literal>{$articleTitle|@count}</literal> (this will print out the number of
|
||||||
<sect3>
|
elements in the $articleTitle array.)
|
||||||
<title>capitalize</title>
|
</para>
|
||||||
<para>
|
|
||||||
This is used to capitalize the first letter of all words in a variable.
|
<sect2 id="language.builtin.modifiers">
|
||||||
</para>
|
<title>Built-in Modifiers</title>
|
||||||
<example>
|
<sect3>
|
||||||
<title>capitalize</title>
|
<title>capitalize</title>
|
||||||
<programlisting>
|
<para>
|
||||||
|
This is used to capitalize the first letter of all words in a variable.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>capitalize</title>
|
||||||
|
<programlisting>
|
||||||
{$articleTitle}
|
{$articleTitle}
|
||||||
{$articleTitle|capitalize}
|
{$articleTitle|capitalize}
|
||||||
|
|
||||||
@@ -1196,19 +1185,20 @@ from dad she hasn't seen in years.
|
|||||||
Blind woman gets new kidney fr
|
Blind woman gets new kidney fr
|
||||||
om dad she hasn't seen in years.</programlisting>
|
om dad she hasn't seen in years.</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
</sect2>
|
</sect2>
|
||||||
<sect2 id="language.combining.modifiers">
|
|
||||||
<title>Combining Modifiers</title>
|
<sect2 id="language.combining.modifiers">
|
||||||
<para>
|
<title>Combining Modifiers</title>
|
||||||
You can combine as many modifiers as you like on a single variable.
|
<para>
|
||||||
This allows the ability to combine the application of different
|
You can apply as many modifiers as you like to a variable. The modifiers
|
||||||
modifiers to a single variable. The modifiers will be applied to
|
will be applied to the variable in the order they are combined, from left
|
||||||
the variable in the order they are combined, from left to right.
|
to right. The modifier have to be separated with a <literal>|</literal>
|
||||||
</para>
|
(pipe) symbol.
|
||||||
<example>
|
</para>
|
||||||
<title>combining modifiers</title>
|
<example>
|
||||||
<programlisting>
|
<title>combining modifiers</title>
|
||||||
|
<programlisting>
|
||||||
{$articleTitle}
|
{$articleTitle}
|
||||||
{$articleTitle|upper|spacify}
|
{$articleTitle|upper|spacify}
|
||||||
{$articleTitle|lower|spacify|truncate}
|
{$articleTitle|lower|spacify|truncate}
|
||||||
@@ -1223,9 +1213,9 @@ S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E
|
|||||||
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
|
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
|
||||||
s m o k e r s a r e p r o d u c t i v e , b u t . . .
|
s m o k e r s a r e p r o d u c t i v e , b u t . . .
|
||||||
s m o k e r s a r e p. . .</programlisting>
|
s m o k e r s a r e p. . .</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect2>
|
</sect2>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="language.builtin.functions">
|
<sect1 id="language.builtin.functions">
|
||||||
<title>Built-in Functions</title>
|
<title>Built-in Functions</title>
|
||||||
@@ -1344,9 +1334,9 @@ s m o k e r s a r e p. . .</programlisting>
|
|||||||
</informaltable>
|
</informaltable>
|
||||||
<para>
|
<para>
|
||||||
This function is used for loading in variables from a
|
This function is used for loading in variables from a
|
||||||
configuration file into the template. You must have the Config_file.class.php
|
configuration file into the template.
|
||||||
file somewhere in your PHP include path for config_load to work properly.
|
See <link linkend="config.files">Config Files</link> for more
|
||||||
See <link linkend="config.files">Config Files</link> for more info.
|
info.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
<title>function config_load</title>
|
<title>function config_load</title>
|
||||||
@@ -2570,14 +2560,14 @@ OUTPUT:
|
|||||||
</sect2>
|
</sect2>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="language.more.functions">
|
<sect1 id="language.custom.functions">
|
||||||
<title>Custom Functions</title>
|
<title>Custom Functions</title>
|
||||||
<para>
|
<para>
|
||||||
Smarty comes with several additional functions that you can
|
Smarty comes with several additional functions that you can
|
||||||
use in the templates.
|
use in the templates.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<sect2 id="language.custom.functions">
|
<sect2 id="language.more.functions">
|
||||||
<title>Additional Functions</title>
|
<title>Additional Functions</title>
|
||||||
<sect3 id="language.function.assign">
|
<sect3 id="language.function.assign">
|
||||||
<title>assign</title>
|
<title>assign</title>
|
||||||
@@ -4086,24 +4076,21 @@ OUTPUT:
|
|||||||
</sect3>
|
</sect3>
|
||||||
</sect2>
|
</sect2>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
</chapter>
|
</chapter>
|
||||||
|
|
||||||
<chapter id="config.files">
|
<chapter id="config.files">
|
||||||
<title>Config Files</title>
|
<title>Config Files</title>
|
||||||
<para>
|
<para>
|
||||||
Config files are handy for designers to manage global template
|
Config files are handy for designers to manage global template
|
||||||
variables from one file. One example is template colors. Normally if
|
variables from one file. One example is template colors. Normally if
|
||||||
you wanted to change the color scheme of an application, you would have
|
you wanted to change the color scheme of an application, you would have
|
||||||
to go through each and every template file and change the colors. With
|
to go through each and every template file and change the colors. With
|
||||||
a config file, the colors can be kept in one place, and only one file
|
a config file, the colors can be kept in one place, and only one file
|
||||||
needs to be updated. Note that to use config files, you must place the
|
needs to be updated.
|
||||||
Config_File.class.php in your PHP include path. Config_File.class.php
|
</para>
|
||||||
comes bundled with Smarty. Smarty will implicitly include the file if
|
<example>
|
||||||
you don't already include it in your application.
|
<title>Example of config file syntax</title>
|
||||||
</para>
|
<programlisting>
|
||||||
<example>
|
|
||||||
<title>Example of config file syntax</title>
|
|
||||||
<programlisting>
|
|
||||||
# global variables
|
# global variables
|
||||||
pageTitle = "Main Menu"
|
pageTitle = "Main Menu"
|
||||||
bodyBgColor = #000000
|
bodyBgColor = #000000
|
||||||
@@ -4125,75 +4112,73 @@ Intro = """This is a value that spans more
|
|||||||
host=my.domain.com
|
host=my.domain.com
|
||||||
db=ADDRESSBOOK
|
db=ADDRESSBOOK
|
||||||
user=php-user
|
user=php-user
|
||||||
pass=foobar
|
pass=foobar</programlisting>
|
||||||
</programlisting>
|
</example>
|
||||||
</example>
|
<para>
|
||||||
<para>
|
Values of config file variables can be in quotes, but not necessary.
|
||||||
Values of config file variables can be in quotes, but not necessary.
|
You can use either single or double quotes. If you have a value that
|
||||||
You can use either single or double quotes. If you have a value that
|
spans more than one line, enclose the entire value with triple quotes
|
||||||
spans more than one line, enclose the entire value with triple quotes
|
("""). You can put comments into config files by any syntax that is not
|
||||||
("""). You can put comments into config files by any syntax that is
|
a valid config file syntax. We recommend using a <literal>#</literal>
|
||||||
not a valid config file syntax. We recommend using a hashmark (#) at the
|
(hash) at the beginning of the line.
|
||||||
beginning of the line.
|
</para>
|
||||||
</para>
|
<para>
|
||||||
<para>
|
This config file example has two sections. Section names are enclosed in
|
||||||
This config file example has two sections. Section names are enclosed
|
brackets []. Section names can be arbitrary strings not containing
|
||||||
in brackets []. The four variables at the top are global
|
<literal>[</literal> or <literal>]</literal> symbols. The four variables
|
||||||
variables, or variables not within a section. These variables are
|
at the top are global variables, or variables not within a section.
|
||||||
always loaded from the config file.
|
These variables are always loaded from the config file. If a particular
|
||||||
If a particular section is loaded, then the global variables and the
|
section is loaded, then the global variables and the variables from that
|
||||||
variables from that section are loaded. If a variable exists both as
|
section are also loaded. If a variable exists both as a global and in a
|
||||||
a global and in a section, the section variable is used. If you name two
|
section, the section variable is used. If you name two variables the
|
||||||
variables the same within a section, the last one will be used.
|
same within a section, the last one will be used.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
Config files are loaded into templates with the built-in function
|
Config files are loaded into templates with the built-in function
|
||||||
called <link linkend="builtin.functions.configload">config_load</link>.
|
<command>config_load</command>.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
You can hide variables or entire sections by prepending the variable
|
You can hide variables or entire sections by prepending the variable
|
||||||
name or section name with a period. This is useful if your
|
name or section name with a period. This is useful if your application
|
||||||
application reads the config files and gets sensitive data from them
|
reads the config files and gets sensitive data from them that the
|
||||||
that the template engine does not need. If you have third parties
|
template engine does not need. If you have third parties doing template
|
||||||
doing template editing, you can be certain that they cannot read
|
editing, you can be certain that they cannot read sensitive data from
|
||||||
sensitive data from the config file by loading it into the template.
|
the config file by loading it into the template.
|
||||||
This is new in Smarty 1.4.6.
|
</para>
|
||||||
</para>
|
</chapter>
|
||||||
</chapter>
|
|
||||||
|
|
||||||
|
|
||||||
<chapter id="chapter.debugging.console">
|
<chapter id="chapter.debugging.console">
|
||||||
<title>Debugging Console</title>
|
<title>Debugging Console</title>
|
||||||
<para>
|
<para>
|
||||||
There is a dubugging console included with Smarty. The console informs you
|
There is a debugging console included with Smarty. The console informs you
|
||||||
of all the included templates, assigned variables and config file variables
|
of all the included templates, assigned variables and config file variables
|
||||||
for the current invocation of the template. A template named "debug.tpl" is
|
for the current invocation of the template. A template named "debug.tpl" is
|
||||||
included with the distribution of Smarty which controls the formatting of
|
included with the distribution of Smarty which controls the formatting of
|
||||||
the console. Set $debugging to true in Smarty, and if needed set $debug_tpl
|
the console. Set $debugging to true in Smarty, and if needed set $debug_tpl
|
||||||
to the template resource path for debug.tpl (this is in $template_dir by
|
to the template resource path for debug.tpl (this is in $template_dir by
|
||||||
default.) When you load the page, a javascript console window should pop up
|
default.) When you load the page, a javascript console window should pop up
|
||||||
and give you the names of all the included templates and assigned variables
|
and give you the names of all the included templates and assigned variables
|
||||||
for the current page. To disable the debugging console, set $debugging to
|
for the current page. To disable the debugging console, set $debugging to
|
||||||
false. You can also temporarily turn on the debugging console by putting
|
false. You can also temporarily turn on the debugging console by putting
|
||||||
SMARTY_DEBUG in the URL if you enable this option with <link
|
SMARTY_DEBUG in the URL if you enable this option with <link
|
||||||
linkend="variable.debugging.ctrl">$debugging_ctrl</link>.
|
linkend="variable.debugging.ctrl">$debugging_ctrl</link>.
|
||||||
</para>
|
</para>
|
||||||
<note>
|
<note>
|
||||||
<title>Technical Note</title>
|
<title>Technical Note</title>
|
||||||
<para>
|
<para>
|
||||||
The debugging console does not work when you use the fetch()
|
The debugging console does not work when you use the fetch()
|
||||||
API, only when using display(). It is a set of javascript statements added
|
API, only when using display(). It is a set of javascript statements added
|
||||||
to the very bottom of the generated template. If you do not like javascript,
|
to the very bottom of the generated template. If you do not like javascript,
|
||||||
you can edit the debug.tpl template to format the output however you like.
|
you can edit the debug.tpl template to format the output however you like.
|
||||||
Debug data is not cached and debug.tpl info is not included in the output of
|
Debug data is not cached and debug.tpl info is not included in the output of
|
||||||
the debug console.
|
the debug console.
|
||||||
</para>
|
</para>
|
||||||
</note>
|
</note>
|
||||||
<note>
|
<note>
|
||||||
<para>
|
<para>
|
||||||
The load times of each template and config file are in seconds, or
|
The load times of each template and config file are in seconds, or
|
||||||
fractions thereof.
|
fractions thereof.
|
||||||
</para>
|
</para>
|
||||||
</note>
|
</note>
|
||||||
</chapter>
|
</chapter>
|
||||||
</part>
|
</part>
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
<part>
|
<part id="smarty.for.programmersr">
|
||||||
<title>Smarty For Programmers</title>
|
<title>Smarty For Programmers</title>
|
||||||
<chapter id="api">
|
<chapter id="api">
|
||||||
<title>Smarty API</title>
|
<title>Smarty API</title>
|
||||||
@@ -139,7 +139,7 @@
|
|||||||
$global_assign to a default value.
|
$global_assign to a default value.
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
<sect2>
|
<sect2 id="variable.compile.check">
|
||||||
<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
|
||||||
@@ -271,7 +271,7 @@
|
|||||||
<para>
|
<para>
|
||||||
NOTE: Embedding PHP code into templates is highly discouraged.
|
NOTE: Embedding PHP code into templates is highly discouraged.
|
||||||
Use <link linkend="bundled.plugins.commands">custom functions</link> or
|
Use <link linkend="bundled.plugins.commands">custom functions</link> or
|
||||||
<link linkend="variable.modifiers">modifiers</link> instead.
|
<link linkend="language.modifiers">modifiers</link> instead.
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
<sect2 id="variable.security">
|
<sect2 id="variable.security">
|
||||||
@@ -994,51 +994,439 @@ echo $output;</programlisting>
|
|||||||
</sect1>
|
</sect1>
|
||||||
</chapter>
|
</chapter>
|
||||||
|
|
||||||
<chapter id="plugins">
|
<chapter id="advanced.features">
|
||||||
<title>Extending Smarty With Plugins</title>
|
<title>Advanced Features</title>
|
||||||
<para>
|
<sect1 id="advanced.features.prefilters">
|
||||||
Version 2.0 introduced the plugin architecture that is used
|
<title>Prefilters</title>
|
||||||
for almost all the customizable functionality of Smarty. This includes:
|
|
||||||
<itemizedlist spacing=compact>
|
|
||||||
<listitem><simpara>functions</simpara></listitem>
|
|
||||||
<listitem><simpara>modifiers</simpara></listitem>
|
|
||||||
<listitem><simpara>compiler functions</simpara></listitem>
|
|
||||||
<listitem><simpara>prefilters</simpara></listitem>
|
|
||||||
<listitem><simpara>postfilters</simpara></listitem>
|
|
||||||
<listitem><simpara>resources</simpara></listitem>
|
|
||||||
<listitem><simpara>inserts</simpara></listitem>
|
|
||||||
</itemizedlist>
|
|
||||||
With the exception of resources, backwards compatibility with the old
|
|
||||||
way of registering handler functions via register_* API is preserved. If
|
|
||||||
you did not use the API but instead modified the class variables
|
|
||||||
<literal>$custom_funcs</literal>, <literal>$custom_mods</literal>, and
|
|
||||||
other ones directly, then you will need to adjust your scripts to either
|
|
||||||
use the API or convert your custom functionality into plugins.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<sect1>
|
|
||||||
<title>How Plugins Work</title>
|
|
||||||
<para>
|
<para>
|
||||||
Plugins are always loaded on demand. Only the specific modifiers,
|
Template prefilters are PHP functions that your templates are ran through
|
||||||
functions, resources, etc invoked in the templates or PHP scripts will
|
before they are compiled. This is good for preprocessing your templates
|
||||||
be loaded. Moreover, each plugin is loaded only once, even if you have
|
to remove unwanted comments, keeping an eye on what people are putting
|
||||||
several different instances of Smarty running within the same request.
|
in their templates, etc. Prefilters are processed in the order they are
|
||||||
</para>
|
<link linkend="api.register.prefilter">registered</link> or loaded from
|
||||||
<para>
|
the <link linkend="plugins.prefilters.postfilters">plugin</link> directory.
|
||||||
Pre/post-filters are a bit of a special case. If a pre/post-filter
|
Smarty will pass the template source code as the first argument, an
|
||||||
plugin is found in the plugins directory, it will always be loaded
|
expect the function to return the resulting template source code.
|
||||||
because these filters are never explicitly mentioned in the template
|
|
||||||
language. This also means that the order in which multiple prefilters
|
|
||||||
or postfilters is executed depends on the way they are named
|
|
||||||
(basically, alphabetical order).
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
There is only one plugins directory (for performance reasons). To
|
|
||||||
install a plugin, simply place it in the directory and Smarty will use
|
|
||||||
it automatically.
|
|
||||||
</para>
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using a template prefilter</title>
|
||||||
|
<programlisting>
|
||||||
|
<?php
|
||||||
|
// put this in your application
|
||||||
|
function remove_dw_comments($tpl_source)
|
||||||
|
{
|
||||||
|
return preg_replace("/<!--#.*-->/U","",$tpl_source);
|
||||||
|
}
|
||||||
|
|
||||||
|
// register the prefilter
|
||||||
|
$smarty->register_prefilter("remove_dw_comments");
|
||||||
|
$smarty->display("index.tpl");
|
||||||
|
?>
|
||||||
|
|
||||||
|
{* Smarty template index.tpl *}
|
||||||
|
<!--# this line will get removed by the prefilter --></programlisting>
|
||||||
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
|
<sect1 id="advanced.features.postfilters">
|
||||||
|
<title>Postfilters</title>
|
||||||
|
<para>
|
||||||
|
Template postfilters are PHP functions that your templates are ran through
|
||||||
|
after they are compiled. Postfilters are processed in the order they are
|
||||||
|
<link linkend="api.register.postfilter">registered</link> or loaded
|
||||||
|
from the <link linkend="plugins.prefilters.postfilters">plugin</link>
|
||||||
|
directory. Smarty will pass the compiled template code as the first
|
||||||
|
argument, an expect the function to return the result of the
|
||||||
|
processing.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using a template postfilter</title>
|
||||||
|
<programlisting>
|
||||||
|
<?php
|
||||||
|
// put this in your application
|
||||||
|
function add_header_comment($tpl_source)
|
||||||
|
{
|
||||||
|
return "<?php echo \"<!-- Created by Smarty! -->\n\" ?>\n".$tpl_source;
|
||||||
|
}
|
||||||
|
|
||||||
|
// register the postfilter
|
||||||
|
$smarty->register_postfilter("add_header_comment");
|
||||||
|
$smarty->display("index.tpl");
|
||||||
|
?>
|
||||||
|
|
||||||
|
{* compiled Smarty template index.tpl *}
|
||||||
|
<!-- Created by Smarty! -->
|
||||||
|
{* rest of template content... *}</programlisting>
|
||||||
|
</example>
|
||||||
|
</sect2>
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1 id="section.template.cache.handler.func">
|
||||||
|
<title>Cache Handler Function</title>
|
||||||
|
<para>
|
||||||
|
As an alternative to using the default file-based caching mechanism, you
|
||||||
|
can specify a custom cache handling function that will be used to read,
|
||||||
|
write and clear cached files.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Create a function in your application that Smarty will use as a
|
||||||
|
cache handler. Set the name of it in the
|
||||||
|
<link linkend="variable.cache.handler.func">$cache_handler_func</link>
|
||||||
|
class variable. Smarty will now use this to handle cached data. The
|
||||||
|
first argument is the action, which will be one of 'read', 'write' and
|
||||||
|
'clear'. The second parameter is the Smarty object. The third parameter
|
||||||
|
is the cached content. Upon a write, Smarty passes the cached content
|
||||||
|
in these parameters. Upon a 'read', Smarty expects your function to
|
||||||
|
accept this parameter by reference and populate it with the cached
|
||||||
|
data. Upon a 'clear', pass a dummy variable here since it is not used.
|
||||||
|
The fourth parameter is the name of the template file (needed for
|
||||||
|
read/write), the fifth parameter is the cache_id (optional), and the
|
||||||
|
sixth is the compile_id (optional).
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>example using MySQL as a cache source</title>
|
||||||
|
<programlisting>
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
|
||||||
|
example usage:
|
||||||
|
|
||||||
|
include('Smarty.class.php');
|
||||||
|
include('mysql_cache_handler.php');
|
||||||
|
|
||||||
|
$smarty = new Smarty;
|
||||||
|
$smarty->cache_handler_func = 'mysql_cache_handler';
|
||||||
|
|
||||||
|
$smarty->display('index.tpl');
|
||||||
|
|
||||||
|
|
||||||
|
mysql database is expected in this format:
|
||||||
|
|
||||||
|
create database SMARTY_CACHE;
|
||||||
|
|
||||||
|
create table CACHE_PAGES(
|
||||||
|
CacheID char(32) PRIMARY KEY,
|
||||||
|
CacheContents MEDIUMTEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null)
|
||||||
|
{
|
||||||
|
// set db host, user and pass here
|
||||||
|
$db_host = 'localhost';
|
||||||
|
$db_user = 'myuser';
|
||||||
|
$db_pass = 'mypass';
|
||||||
|
$db_name = 'SMARTY_CACHE';
|
||||||
|
$use_gzip = false;
|
||||||
|
|
||||||
|
// create unique cache id
|
||||||
|
$CacheID = md5($tpl_file.$cache_id.$compile_id);
|
||||||
|
|
||||||
|
if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
|
||||||
|
$smarty_obj->_trigger_error_msg("cache_handler: could not connect to database");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mysql_select_db($db_name);
|
||||||
|
|
||||||
|
switch ($action) {
|
||||||
|
case 'read':
|
||||||
|
// save cache to database
|
||||||
|
$results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
|
||||||
|
if(!$results) {
|
||||||
|
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
||||||
|
}
|
||||||
|
$row = mysql_fetch_array($results,MYSQL_ASSOC);
|
||||||
|
|
||||||
|
if($use_gzip && function_exists("gzuncompress")) {
|
||||||
|
$cache_contents = gzuncompress($row["CacheContents"]);
|
||||||
|
} else {
|
||||||
|
$cache_contents = $row["CacheContents"];
|
||||||
|
}
|
||||||
|
$return = $results;
|
||||||
|
break;
|
||||||
|
case 'write':
|
||||||
|
// save cache to database
|
||||||
|
|
||||||
|
if($use_gzip && function_exists("gzcompress")) {
|
||||||
|
// compress the contents for storage efficiency
|
||||||
|
$contents = gzcompress($cache_content);
|
||||||
|
} else {
|
||||||
|
$contents = $cache_content;
|
||||||
|
}
|
||||||
|
$results = mysql_query("replace into CACHE_PAGES values(
|
||||||
|
'$CacheID',
|
||||||
|
'".addslashes($contents)."')
|
||||||
|
");
|
||||||
|
if(!$results) {
|
||||||
|
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
||||||
|
}
|
||||||
|
$return = $results;
|
||||||
|
break;
|
||||||
|
case 'clear':
|
||||||
|
// clear cache info
|
||||||
|
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
|
||||||
|
// clear them all
|
||||||
|
$results = mysql_query("delete from CACHE_PAGES");
|
||||||
|
} else {
|
||||||
|
$results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
|
||||||
|
}
|
||||||
|
if(!$results) {
|
||||||
|
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
||||||
|
}
|
||||||
|
$return = $results;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// error, unknown action
|
||||||
|
$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
|
||||||
|
$return = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mysql_close($link);
|
||||||
|
return $return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?></programlisting>
|
||||||
|
</example>
|
||||||
|
</sect1>
|
||||||
|
|
||||||
|
<sect1 id="template.resources">
|
||||||
|
<title>Resources</title>
|
||||||
|
<para>
|
||||||
|
The templates may come from a variety of sources. When you display or
|
||||||
|
fetch a template, or when you include a template from within another
|
||||||
|
template, you supply a resource type, followed by the appropriate path
|
||||||
|
and template name.
|
||||||
|
</para>
|
||||||
|
<sect2 id="templates.from.template.dir">
|
||||||
|
<title>Templates from $template_dir</title>
|
||||||
|
<para>
|
||||||
|
Templates from the $template_dir do not require a template
|
||||||
|
resource, although you can use the file: resource for consistancy.
|
||||||
|
Just supply the path to the template you want to use relative to
|
||||||
|
the $template_dir root directory.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using templates from $template_dir</title>
|
||||||
|
<programlisting>
|
||||||
|
// from PHP script
|
||||||
|
$smarty->display("index.tpl");
|
||||||
|
$smarty->display("admin/menu.tpl");
|
||||||
|
$smarty->display("file:admin/menu.tpl"); // same as one above
|
||||||
|
|
||||||
|
{* from within Smarty template *}
|
||||||
|
{include file="index.tpl"}
|
||||||
|
{include file="file:index.tpl"} {* same as one above *}</programlisting>
|
||||||
|
</example>
|
||||||
|
</sect2>
|
||||||
|
<sect2 id="templates.from.any.dir">
|
||||||
|
<title>Templates from any directory</title>
|
||||||
|
<para>
|
||||||
|
Templates outside of the $template_dir require the file: template
|
||||||
|
resource type, followed by the absolute path and name of the
|
||||||
|
template.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using templates from any directory</title>
|
||||||
|
<programlisting>
|
||||||
|
// from PHP script
|
||||||
|
$smarty->display("file:/export/templates/index.tpl");
|
||||||
|
$smarty->display("file:/path/to/my/templates/menu.tpl");
|
||||||
|
|
||||||
|
{* from within Smarty template *}
|
||||||
|
{include file="file:/usr/local/share/templates/navigation.tpl"}</programlisting>
|
||||||
|
</example>
|
||||||
|
|
||||||
|
<sect3>
|
||||||
|
<title>Windows Filepaths</title>
|
||||||
|
<para>
|
||||||
|
If you are using a Windows machine, filepaths usually include a
|
||||||
|
drive letter (C:) at the beginning of the pathname. Be sure to use
|
||||||
|
"file:" in the path to avoid namespace conflicts and get the
|
||||||
|
desired results.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using templates from windows file paths</title>
|
||||||
|
<programlisting>
|
||||||
|
// from PHP script
|
||||||
|
$smarty->display("file:C:/export/templates/index.tpl");
|
||||||
|
$smarty->display("file:F:/path/to/my/templates/menu.tpl");
|
||||||
|
|
||||||
|
{* from within Smarty template *}
|
||||||
|
{include file="file:D:/usr/local/share/templates/navigation.tpl"}</programlisting>
|
||||||
|
</example>
|
||||||
|
</sect3>
|
||||||
|
</sect2>
|
||||||
|
|
||||||
|
<sect2 id="templates.from.elsewhere">
|
||||||
|
<title>Templates from other sources</title>
|
||||||
|
<para>
|
||||||
|
You can retrieve templates using whatever possible source you can
|
||||||
|
access with PHP: databases, sockets, LDAP, and so on. You do this
|
||||||
|
by writing resource plugin functions and registering them with
|
||||||
|
Smarty.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
See <link linkend="plugins.resources">resource plugins</link>
|
||||||
|
section for more information on the functions you are supposed
|
||||||
|
to provide.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
Note that you cannot override the built-in
|
||||||
|
<literal>file</literal> resource, but you can provide a resource
|
||||||
|
that fetches templates from the file system in some other way by
|
||||||
|
registering under another resource name.
|
||||||
|
</para>
|
||||||
|
</note>
|
||||||
|
<example>
|
||||||
|
<title>using custom resources</title>
|
||||||
|
<programlisting>
|
||||||
|
// from PHP script
|
||||||
|
|
||||||
|
// put these function somewhere in your application
|
||||||
|
function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)
|
||||||
|
{
|
||||||
|
// do database call here to fetch your template,
|
||||||
|
// populating $tpl_source
|
||||||
|
$sql = new SQL;
|
||||||
|
$sql->query("select tpl_source
|
||||||
|
from my_table
|
||||||
|
where tpl_name='$tpl_name'");
|
||||||
|
if ($sql->num_rows) {
|
||||||
|
$tpl_source = $sql->record['tpl_source'];
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
|
||||||
|
{
|
||||||
|
// do database call here to populate $tpl_timestamp.
|
||||||
|
$sql = new SQL;
|
||||||
|
$sql->query("select tpl_timestamp
|
||||||
|
from my_table
|
||||||
|
where tpl_name='$tpl_name'");
|
||||||
|
if ($sql->num_rows) {
|
||||||
|
$tpl_timestamp = $sql->record['tpl_timestamp'];
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function db_get_secure($tpl_name, &$smarty_obj)
|
||||||
|
{
|
||||||
|
// assume all templates are secure
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function db_get_trusted($tpl_name, &$smarty_obj)
|
||||||
|
{
|
||||||
|
// not used for templates
|
||||||
|
}
|
||||||
|
|
||||||
|
// register the resource name "db"
|
||||||
|
$smarty->register_resource("db", array("db_get_template",
|
||||||
|
"db_get_timestamp",
|
||||||
|
"db_get_secure",
|
||||||
|
"db_get_trusted"));
|
||||||
|
|
||||||
|
// using resource from php script
|
||||||
|
$smarty->display("db:index.tpl");
|
||||||
|
|
||||||
|
{* using resource from within Smarty template *}
|
||||||
|
{include file="db:/extras/navigation.tpl"}</programlisting>
|
||||||
|
</example>
|
||||||
|
</sect2>
|
||||||
|
|
||||||
|
<sect2 id="default.template.handler.function">
|
||||||
|
<title>Default template handler function</title>
|
||||||
|
<para>
|
||||||
|
You can specify a function that is used to retrieve template
|
||||||
|
contents in the event the template cannot be retrieved from its
|
||||||
|
resource. One use of this is to create templates that do not exist
|
||||||
|
on-the-fly.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>using the default template handler function</title>
|
||||||
|
<programlisting>
|
||||||
|
<?php
|
||||||
|
// put this function somewhere in your application
|
||||||
|
|
||||||
|
function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
|
||||||
|
{
|
||||||
|
if( $resource_type == 'file' ) {
|
||||||
|
if ( ! is_readable ( $resource_name )) {
|
||||||
|
// create the template file, return contents.
|
||||||
|
$template_source = "This is a new template.";
|
||||||
|
$template_timestamp = time();
|
||||||
|
$smarty_obj->_write_file($resource_name,$template_source);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not a file
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the default handler
|
||||||
|
$smarty->default_template_handler_func = 'make_template';
|
||||||
|
?></programlisting>
|
||||||
|
</example>
|
||||||
|
</sect2>
|
||||||
|
</sect1>
|
||||||
|
</chapter>
|
||||||
|
|
||||||
|
<chapter id="plugins">
|
||||||
|
<title>Extending Smarty With Plugins</title>
|
||||||
|
<para>
|
||||||
|
Version 2.0 introduced the plugin architecture that is used
|
||||||
|
for almost all the customizable functionality of Smarty. This includes:
|
||||||
|
<itemizedlist spacing=compact>
|
||||||
|
<listitem><simpara>functions</simpara></listitem>
|
||||||
|
<listitem><simpara>modifiers</simpara></listitem>
|
||||||
|
<listitem><simpara>compiler functions</simpara></listitem>
|
||||||
|
<listitem><simpara>prefilters</simpara></listitem>
|
||||||
|
<listitem><simpara>postfilters</simpara></listitem>
|
||||||
|
<listitem><simpara>resources</simpara></listitem>
|
||||||
|
<listitem><simpara>inserts</simpara></listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
With the exception of resources, backwards compatibility with the old
|
||||||
|
way of registering handler functions via register_* API is preserved. If
|
||||||
|
you did not use the API but instead modified the class variables
|
||||||
|
<literal>$custom_funcs</literal>, <literal>$custom_mods</literal>, and
|
||||||
|
other ones directly, then you will need to adjust your scripts to either
|
||||||
|
use the API or convert your custom functionality into plugins.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<sect1>
|
||||||
|
<title>How Plugins Work</title>
|
||||||
|
<para>
|
||||||
|
Plugins are always loaded on demand. Only the specific modifiers,
|
||||||
|
functions, resources, etc invoked in the templates or PHP scripts will
|
||||||
|
be loaded. Moreover, each plugin is loaded only once, even if you have
|
||||||
|
several different instances of Smarty running within the same request.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Pre/post-filters are a bit of a special case. If a pre/post-filter
|
||||||
|
plugin is found in the plugins directory, it will always be loaded
|
||||||
|
because these filters are never explicitly mentioned in the template
|
||||||
|
language. This also means that the order in which multiple prefilters
|
||||||
|
or postfilters is executed depends on the way they are named
|
||||||
|
(basically, alphabetical order).
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
There is only one plugins directory (for performance reasons). To
|
||||||
|
install a plugin, simply place it in the directory and Smarty will use
|
||||||
|
it automatically.
|
||||||
|
</para>
|
||||||
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="plugins.naming.conventions">
|
<sect1 id="plugins.naming.conventions">
|
||||||
<title>Naming Conventions</title>
|
<title>Naming Conventions</title>
|
||||||
<para>
|
<para>
|
||||||
@@ -1376,7 +1764,8 @@ echo 'index.tpl compiled at 2002-02-20 20:02';
|
|||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
<sect1 id="plugins.prefilters.postfilters"><title>Prefilters/Postfilters</title>
|
<sect1 id="plugins.prefilters.postfilters">
|
||||||
|
<title>Prefilters/Postfilters</title>
|
||||||
<para>
|
<para>
|
||||||
Prefilter and postfilter plugins are very similar in concept; where
|
Prefilter and postfilter plugins are very similar in concept; where
|
||||||
they differ is in the execution -- more precisely the time of their
|
they differ is in the execution -- more precisely the time of their
|
||||||
@@ -1640,412 +2029,4 @@ function smarty_insert_time($params, &$smarty)
|
|||||||
</example>
|
</example>
|
||||||
</sect1>
|
</sect1>
|
||||||
</chapter>
|
</chapter>
|
||||||
<chapter id="advanced.features">
|
|
||||||
<title>Advanced Features</title>
|
|
||||||
<sect1 id="advanced.features.prefilters">
|
|
||||||
<title>Prefilters</title>
|
|
||||||
<para>
|
|
||||||
Template prefilters are PHP functions that your templates are ran through
|
|
||||||
before they are compiled. This is good for preprocessing your templates
|
|
||||||
to remove unwanted comments, keeping an eye on what people are putting
|
|
||||||
in their templates, etc. Prefilters are processed in the order they are
|
|
||||||
<link linkend="api.register.prefilter">registered</link>.
|
|
||||||
</para>
|
|
||||||
<sect2>
|
|
||||||
<title>Example of a Template Prefilter</title>
|
|
||||||
<para>
|
|
||||||
Create a function in your application that Smarty will use as a
|
|
||||||
template prefilter. Smarty will pass the template source code as the
|
|
||||||
first argument, an expect the function to return the resulting
|
|
||||||
template source code.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
NOTE: As of Smarty 1.4.5, the Smarty object is passed as the second
|
|
||||||
argument. This way you can reference and modify information in the
|
|
||||||
Smarty object from within the prefilter function.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>using a template prefilter</title>
|
|
||||||
<programlisting>
|
|
||||||
// put this in your application, or in Smarty.addons.php
|
|
||||||
function remove_dw_comments($tpl_source) {
|
|
||||||
return preg_replace("/<!--#.*-->/U","",$tpl_source);
|
|
||||||
}
|
|
||||||
|
|
||||||
// register the prefilter
|
|
||||||
$smarty->register_prefilter("remove_dw_comments");
|
|
||||||
$smarty->display("index.tpl");
|
|
||||||
|
|
||||||
{* from within Smarty template *}
|
|
||||||
<!--# this line will get removed by the prefilter --></programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
</sect1>
|
|
||||||
<sect1 id="advanced.features.postfilters">
|
|
||||||
<title>Postfilters</title>
|
|
||||||
<para>
|
|
||||||
Template postfilters are PHP functions that your templates are ran through
|
|
||||||
after they are compiled. Postfilters are processed in the order they are
|
|
||||||
<link linkend="api.register.postfilter">registered</link>.
|
|
||||||
</para>
|
|
||||||
<sect2>
|
|
||||||
<title>Example of a Template Postfilter</title>
|
|
||||||
<para>
|
|
||||||
Create a function in your application that Smarty will use as a
|
|
||||||
template postfilter. Smarty will pass the template source code as the
|
|
||||||
first argument, an expect the function to return the resulting
|
|
||||||
template source code.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
The Smarty object is passed as the second argument. This way you
|
|
||||||
can reference and modify information in the Smarty object from
|
|
||||||
within the prefilter function.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>using a template postfilter</title>
|
|
||||||
<programlisting>
|
|
||||||
// this program puts a
|
|
||||||
// put this in your application, or in Smarty.addons.php
|
|
||||||
function add_header_comment($tpl_source) {
|
|
||||||
return "<?php echo \"<!-- Created by Smarty! -->\n\" ?>\n".$tpl_source;
|
|
||||||
}
|
|
||||||
|
|
||||||
// register the postfilter
|
|
||||||
$smarty->register_postfilter("add_header_comment");
|
|
||||||
$smarty->display("index.tpl");
|
|
||||||
|
|
||||||
{* from within Smarty template *}
|
|
||||||
<!-- Created by Smarty! -->
|
|
||||||
{* rest of template content... *}</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
</sect1>
|
|
||||||
<sect1 id="section.template.cache.handler.func">
|
|
||||||
<title>Cache Handling Function</title>
|
|
||||||
<para>
|
|
||||||
As an alternative to using the default file-based caching mechanism, you
|
|
||||||
can specify a custom cache handling function that will be used to read,
|
|
||||||
write and clear cached files.
|
|
||||||
</para>
|
|
||||||
<sect2>
|
|
||||||
<title>Example of a Template Cache Handling Function</title>
|
|
||||||
<para>
|
|
||||||
Create a function in your application that Smarty will use as a
|
|
||||||
cache handler. Set the name of it in the $cache_handler_func class
|
|
||||||
variable. Smarty will now use this to handle cached data. The first
|
|
||||||
argument is the action, which will be one of 'read', 'write' and
|
|
||||||
'clear'. The second parameter is the smarty object. The third
|
|
||||||
parameter is the cached content. Upon a write, Smarty passes the
|
|
||||||
cached content in these parameters. Upon a 'read', Smarty expects
|
|
||||||
your function to pass this by reference and populate it with the
|
|
||||||
cached data. Upon a 'clear', pass a dummy variable here since it is
|
|
||||||
not used. The fourth parameter is the name of the template file
|
|
||||||
(needed for read/write), the fifth parameter is the cache_id
|
|
||||||
(optional), and the sixth is the compile_id (optional)
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>example using MySQL as a cache source</title>
|
|
||||||
<programlisting>
|
|
||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
example usage:
|
|
||||||
|
|
||||||
include('Smarty.class.php');
|
|
||||||
include('mysql_cache_handler.php');
|
|
||||||
|
|
||||||
$smarty = new Smarty;
|
|
||||||
$smarty->cache_handler_func='mysql_cache_handler';
|
|
||||||
|
|
||||||
$smarty->display('index.tpl');
|
|
||||||
|
|
||||||
|
|
||||||
mysql database is expected in this format:
|
|
||||||
|
|
||||||
create database SMARTY_CACHE;
|
|
||||||
|
|
||||||
create table CACHE_PAGES(
|
|
||||||
CacheID char(32) PRIMARY KEY,
|
|
||||||
CacheContents MEDIUMTEXT NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null) {
|
|
||||||
// set db host, user and pass here
|
|
||||||
$db_host = 'localhost';
|
|
||||||
$db_user = 'myuser';
|
|
||||||
$db_pass = 'mypass';
|
|
||||||
$db_name = 'SMARTY_CACHE';
|
|
||||||
$use_gzip = false;
|
|
||||||
|
|
||||||
// create unique cache id
|
|
||||||
$CacheID = md5($tpl_file.$cache_id.$compile_id);
|
|
||||||
|
|
||||||
if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
|
|
||||||
$smarty_obj->_trigger_error_msg("cache_handler: could not connect to database");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
mysql_select_db($db_name);
|
|
||||||
|
|
||||||
switch ($action) {
|
|
||||||
case 'read':
|
|
||||||
// save cache to database
|
|
||||||
$results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
|
|
||||||
if(!$results) {
|
|
||||||
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
|
||||||
}
|
|
||||||
$row = mysql_fetch_array($results,MYSQL_ASSOC);
|
|
||||||
|
|
||||||
if($use_gzip && function_exists("gzuncompress")) {
|
|
||||||
$cache_contents = gzuncompress($row["CacheContents"]);
|
|
||||||
} else {
|
|
||||||
$cache_contents = $row["CacheContents"];
|
|
||||||
}
|
|
||||||
$return = $results;
|
|
||||||
break;
|
|
||||||
case 'write':
|
|
||||||
// save cache to database
|
|
||||||
|
|
||||||
if($use_gzip && function_exists("gzcompress")) {
|
|
||||||
// compress the contents for storage efficiency
|
|
||||||
$contents = gzcompress($cache_content);
|
|
||||||
} else {
|
|
||||||
$contents = $cache_content;
|
|
||||||
}
|
|
||||||
$results = mysql_query("replace into CACHE_PAGES values(
|
|
||||||
'$CacheID',
|
|
||||||
'".addslashes($contents)."')
|
|
||||||
");
|
|
||||||
if(!$results) {
|
|
||||||
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
|
||||||
}
|
|
||||||
$return = $results;
|
|
||||||
break;
|
|
||||||
case 'clear':
|
|
||||||
// clear cache info
|
|
||||||
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
|
|
||||||
// clear them all
|
|
||||||
$results = mysql_query("delete from CACHE_PAGES");
|
|
||||||
} else {
|
|
||||||
$results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
|
|
||||||
}
|
|
||||||
if(!$results) {
|
|
||||||
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
|
|
||||||
}
|
|
||||||
$return = $results;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// error, unknown action
|
|
||||||
$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
|
|
||||||
$return = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
mysql_close($link);
|
|
||||||
return $return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
</sect1>
|
|
||||||
<sect1 id="template.resources">
|
|
||||||
<title>Resources</title>
|
|
||||||
<para>
|
|
||||||
Your templates may come from a variety of sources. When you display or
|
|
||||||
fetch a template, or when you include a template from within another
|
|
||||||
template, you supply a resource type, followed by the appropriate path
|
|
||||||
and template name.
|
|
||||||
</para>
|
|
||||||
<sect2>
|
|
||||||
<title>Templates from $template_dir</title>
|
|
||||||
<para>
|
|
||||||
Templates from the $template_dir do not require a template
|
|
||||||
resource, although you can use the file: resource for consistancy.
|
|
||||||
Just supply the path to the template you want to use relative to
|
|
||||||
the $template_dir root directory.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>using templates from $template_dir</title>
|
|
||||||
<programlisting>
|
|
||||||
// from PHP script
|
|
||||||
$smarty->display("index.tpl");
|
|
||||||
$smarty->display("admin/menu.tpl");
|
|
||||||
$smarty->display("file:admin/menu.tpl"); // same as one above
|
|
||||||
|
|
||||||
{* from within Smarty template *}
|
|
||||||
{include file="index.tpl"}
|
|
||||||
{include file="file:index.tpl"} {* same as one above *}</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
<sect2>
|
|
||||||
<title>Templates from any directory</title>
|
|
||||||
<para>
|
|
||||||
Templates outside of the $template_dir require the file: template
|
|
||||||
resource type, followed by the absolute path and name of the
|
|
||||||
template.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
|
|
||||||
<title>using templates from any directory</title>
|
|
||||||
<programlisting>
|
|
||||||
// from PHP script
|
|
||||||
$smarty->display("file:/export/templates/index.tpl");
|
|
||||||
$smarty->display("file:/path/to/my/templates/menu.tpl");
|
|
||||||
|
|
||||||
{* from within Smarty template *}
|
|
||||||
{include file="file:/usr/local/share/templates/navigation.tpl"}</programlisting>
|
|
||||||
</example>
|
|
||||||
<sect3>
|
|
||||||
<title>Windows Filepaths</title>
|
|
||||||
<para>
|
|
||||||
If you are using a Windows machine, filepaths usually include a
|
|
||||||
drive letter (C:) at the beginning of the pathname. Be sure to use
|
|
||||||
"file:" in the path to avoid namespace conflicts and get the
|
|
||||||
desired results.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
<title>using templates from windows file paths</title>
|
|
||||||
<programlisting>
|
|
||||||
// from PHP script
|
|
||||||
$smarty->display("file:C:/export/templates/index.tpl");
|
|
||||||
$smarty->display("file:F:/path/to/my/templates/menu.tpl");
|
|
||||||
|
|
||||||
{* from within Smarty template *}
|
|
||||||
{include file="file:D:/usr/local/share/templates/navigation.tpl"}</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect3>
|
|
||||||
</sect2>
|
|
||||||
|
|
||||||
<sect2>
|
|
||||||
<title>Templates from other sources</title>
|
|
||||||
<para>
|
|
||||||
You can retrieve templates using whatever possible source you can
|
|
||||||
access with PHP: databases, sockets, LDAP, and so on. You do this
|
|
||||||
by writing resource plugin functions and registering them with
|
|
||||||
Smarty.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
See <link linkend="plugins.resources">resource plugins</link>
|
|
||||||
section for more information on the functions you are supposed
|
|
||||||
to provide.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<note>
|
|
||||||
<para>
|
|
||||||
Note that you cannot override the built-in
|
|
||||||
<literal>file</literal> resource, but you can provide a resource
|
|
||||||
that fetches templates from the file system in some other way by
|
|
||||||
registering under another resource name.
|
|
||||||
</para>
|
|
||||||
</note>
|
|
||||||
<example>
|
|
||||||
<title>using custom resources</title>
|
|
||||||
<programlisting>
|
|
||||||
// from PHP script
|
|
||||||
|
|
||||||
// put these function somewhere in your application
|
|
||||||
function db_get_template ($tpl_name, &tpl_source, &$smarty_obj)
|
|
||||||
{
|
|
||||||
// do database call here to fetch your template,
|
|
||||||
// populating $tpl_source
|
|
||||||
$sql = new SQL;
|
|
||||||
$sql->query("select tpl_source
|
|
||||||
from my_table
|
|
||||||
where tpl_name='$tpl_name'");
|
|
||||||
if ($sql->num_rows) {
|
|
||||||
$tpl_source = $sql->record['tpl_source'];
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
|
|
||||||
{
|
|
||||||
// do database call here to populate $tpl_timestamp.
|
|
||||||
$sql = new SQL;
|
|
||||||
$sql->query("select tpl_timestamp
|
|
||||||
from my_table
|
|
||||||
where tpl_name='$tpl_name'");
|
|
||||||
if ($sql->num_rows) {
|
|
||||||
$tpl_timestamp = $sql->record['tpl_timestamp'];
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function db_get_secure($tpl_name, &$smarty_obj)
|
|
||||||
{
|
|
||||||
// assume all templates are secure
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function db_get_trusted($tpl_name, &$smarty_obj)
|
|
||||||
{
|
|
||||||
// not used for templates
|
|
||||||
}
|
|
||||||
|
|
||||||
// register the resource name "db"
|
|
||||||
$smarty->register_resource("db", array("db_get_template",
|
|
||||||
"db_get_timestamp",
|
|
||||||
"db_get_secure",
|
|
||||||
"db_get_trusted"));
|
|
||||||
|
|
||||||
// using resource from php script
|
|
||||||
$smarty->display("db:index.tpl");
|
|
||||||
|
|
||||||
{* using resource from within Smarty template *}
|
|
||||||
{include file="db:/extras/navigation.tpl"}</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
|
|
||||||
<sect2>
|
|
||||||
<title>Default Template Handler Function</title>
|
|
||||||
<para>
|
|
||||||
You can specify a function that is used to retrieve template
|
|
||||||
contents in the event the template cannot be retrieved from its
|
|
||||||
resource. One use of this is to create templates that do not exist
|
|
||||||
on-the-fly.
|
|
||||||
</para>
|
|
||||||
<example>
|
|
||||||
<title>using the default template handler function</title>
|
|
||||||
<programlisting>
|
|
||||||
// from PHP script
|
|
||||||
|
|
||||||
// put this function somewhere in your application
|
|
||||||
|
|
||||||
function make_template ($resource_type, $resource_name, &$template_source,
|
|
||||||
&$template_timestamp, &$smarty_obj) {
|
|
||||||
|
|
||||||
if( $resource_type == 'file' ) {
|
|
||||||
if ( ! is_readable ( $resource_name )) {
|
|
||||||
// create the template file, return contents.
|
|
||||||
$template_source = "This is a new template.";
|
|
||||||
$template_timestamp = time();
|
|
||||||
$smarty_obj->_write_file($resource_name,$template_source);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// not a file
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the default handler
|
|
||||||
$smarty->default_template_handler_func = 'make_template';</programlisting>
|
|
||||||
</example>
|
|
||||||
</sect2>
|
|
||||||
</sect1>
|
|
||||||
</chapter>
|
|
||||||
|
|
||||||
</part>
|
</part>
|
||||||
|
Reference in New Issue
Block a user