2001-01-25 16:22:44 +00:00
<!dOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
2001-01-04 20:33:27 +00:00
<book>
<bookinfo>
<title>Smarty - the compiling PHP template engine</title>
2001-01-18 20:41:43 +00:00
<author>
<firstname>Monte</firstname><surname>Ohrt</surname>
<affiliation>
<address><email>monte@ispi.net</email></address>
</affiliation>
</author>
<author>
<firstname>Andrei</firstname><surname>Zmievski</surname>
<affiliation>
<address><email>andrei@ispi.net</email></address>
</affiliation>
</author>
2001-01-31 22:42:05 +00:00
<edition>Version 1.2.3</edition>
2001-01-26 17:40:58 +00:00
<copyright><year>2001</year><holder>ispi of Lincoln, Inc.</holder></copyright>
2001-01-04 20:33:27 +00:00
</bookinfo>
<chapter>
<title>Overview</title>
<para>
It is undoubtedly one of the most asked questions on the PHP mailing
lists: how do I make my PHP scripts independent of the layout? While
PHP is billed as "HTML embedded scripting language", after writing a
2001-01-18 20:41:43 +00:00
couple of projects that mixed PHP and HTML freely one comes up with the
idea that separation of form and content is a Good Thing [TM]. In
2001-01-04 20:33:27 +00:00
addition, in many companies the roles of layout designer and programmer
2001-01-18 20:41:43 +00:00
are separate. Consequently, the search for a templating solution
ensues.
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-01-12 21:18:41 +00:00
In our company for example, the development of an application goes on
as follows: After the requirements docs are done, the interface
2001-01-12 20:39:27 +00:00
designer makes mockups of the interface and gives them to the
programmer. The programmer implements business logic in PHP and uses
interface mockups to create skeleton templates. The project is then
handed off to the HTML designer/web page layout person who brings the
templates up to their full glory. The project may go back and forth
between programming/HTML a couple of times. Thus, it's important to
have good template support because programmers don't want anything to
do with HTML and don't want HTML designers mucking around with PHP
2001-01-18 20:41:43 +00:00
code. Designers need support for config files, dynamic blocks and other
stuff, but they don't want to have to deal with intricacies of the PHP
programming language.
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-01-12 20:39:27 +00:00
Looking at many templating solutions available for PHP today, most of
them provide a rudimentary way of substituting variables into templates
2001-01-18 20:41:43 +00:00
and do a limited form of dynamic block functionality. But our needs
required a bit more than that. We didn't want programmers to be dealing
with HTML layout at ALL, but this was almost inevitable. For instance,
if a designer wanted background colors to alternate on dynamic blocks,
this had to be worked out with the programmer in advance. We also
needed designers to be able to use their own configuration files, and
pull variables from them into the templates. The list goes on.
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-01-12 20:39:27 +00:00
We started out writing out a spec for a template engine about a year
ago. After finishing the spec, we began to work on a template engine
written in C that would hopefully be accepted for inclusion with PHP.
Not only did we run into many complicated technical barriers, but there
was also much heated debate about exactly what a template engine should
and should not do. From this experience, we decided that the template
engine should be written in PHP as a class, for anyone to use as they
see fit. So we wrote an engine that did just that and
<emphasis>SmartTemplate</emphasis> came into existence (note: this
class was never submitted to the public). It was a class that did
almost everything we wanted: regular variable substitution, supported
including other templates, integration with config files, embedding PHP
code, limited 'if' statement functionality and much more robust dynamic
blocks which could be multiply nested. It did all this with regular
expressions and the code turned out to be rather, shall we say,
impenetrable. It was also noticably slow in large applications from all
the parsing and regular expression work it had to do on each
2001-01-18 20:41:43 +00:00
invocation. The biggest problem from a programmer's point of view was
all the necessary work in the PHP script to setup and process templates
and dynamic blocks. How do we make this easier?
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-01-12 20:39:27 +00:00
Then came the vision of what ultimately became Smarty. We know how fast
PHP code is without the overhead of template parsing. We also know how
meticulous and overbearing the PHP language may look to the average
designer, and this could be masked with a much simpler templating
syntax. So what if we combined the two strengths? Thus, Smarty was
born...
2001-01-04 20:33:27 +00:00
</para>
<sect1><title>What is Smarty?</title>
<para>
Smarty is a template engine for PHP. One of the unique aspects about
Smarty that sets it apart from other templating solutions is that it
2001-01-12 20:39:27 +00:00
compiles the templates into native PHP scripts upon the first
2001-01-04 20:33:27 +00:00
invocation. After that, it merely executes the compiled PHP scripts.
Therefore, there is no costly template file parsing for each request.
</para>
<para>
Some of Smarty's features:
</para>
<itemizedlist>
<listitem><para>It is extremely fast.</para></listitem>
2001-01-12 20:39:27 +00:00
<listitem><para>It is efficient since the PHP parser does the
2001-01-04 20:33:27 +00:00
dirty work.</para></listitem>
<listitem><para>No template parsing overhead, only compiles once.</para></listitem>
<listitem><para>It is smart about recompiling only the template
files that have changed.</para></listitem>
2001-01-12 20:39:27 +00:00
<listitem><para>You can make <link linkend="custom.functions">custom
functions</link> and custom <link linkend="variable.modifiers">variable
modifiers</link>, so the template language is extremely extensible.</para></listitem>
2001-01-04 20:33:27 +00:00
<listitem><para>Configurable template delimiter tag syntax, so you can use
{}, {{}}, <!--{}-->, or whatever you like.</para></listitem>
2001-01-12 20:39:27 +00:00
<listitem><para>The if/elseif/else/endif constructs are passed to the
PHP parser, so the if expression syntax can be as simple or as complex
as you like.</para></listitem>
<listitem><para>Unlimited nesting of sections, ifs, etc. allowed.</para></listitem>
<listitem><para>It is possible to embed PHP code right in your template files,
2001-01-18 20:41:43 +00:00
although this may not be needed (nor recommended)
2001-01-12 21:18:41 +00:00
since the engine is so customizable.</para></listitem>
2001-02-01 17:40:59 +00:00
<listitem><para>Built-in caching support (new in 1.3.0)</para></listitem>
2001-02-01 20:08:04 +00:00
</itemizedlist>
2001-01-04 20:33:27 +00:00
</sect1>
<sect1>
<title>How Smarty works</title>
2001-01-12 20:39:27 +00:00
<sect2><title>Compiling</title>
2001-01-04 20:33:27 +00:00
<para>
Smarty compiles the templates into native PHP code on-the-fly. The actual
PHP scripts that are generated are created implicitly, so theoretically you
should never have to worry about touching these files, or even know of their
2001-01-12 20:39:27 +00:00
existence. The exception to this is debugging Smarty template syntax errors,
2001-01-04 20:33:27 +00:00
discussed later in this document.
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="section.caching">
<title>Caching</title>
2001-01-04 20:33:27 +00:00
<para>
2001-02-07 23:09:32 +00:00
Smarty can cache the output of your generated templates. By default
this is disabled. If you <link linkend="setting.caching">enable
caching</link>, Smarty will store a copy of the generated template
output, and use this until the copy <link
linkend="setting.cache.lifetime">expires</link>, regenerating a new
one. The default cache expire time is one hour, and can be
configured from the class. The exception to the rule is the <link
linkend="function.insert">insert</link> tag. Anything generated by
the insert tag is not cached, but run dynamically on every
invocation, even within cached content.
2001-01-04 20:33:27 +00:00
</para>
2001-02-07 23:09:32 +00:00
<para>
TECHNICAL NOTE: Any time you change a template, change values in
config files or change the content that gets displayed in a
template, you must either clear the caches that are affected, or
wait for the cache to expire to see the results of the changes. You
can either do this manually by deleting files from the cache
directory, or programatically with <link
linkend="api.clear.cache">clear_cache</link> or <link
linkend="api.clear.all.cache">clear_all_cache</link>.
</para>
2001-01-04 20:33:27 +00:00
</sect2>
</sect1>
</chapter>
<chapter>
<title>Installation</title>
<sect1>
<title>Requirements</title>
<para>
2001-01-29 16:36:07 +00:00
Smarty requires PHP 4.0.4pl1 or later. See the
2001-01-18 20:41:43 +00:00
<link linkend="bugs">BUGS</link> section for caveats.
2001-01-04 20:33:27 +00:00
</para>
</sect1>
<sect1>
<title>Installing Smarty</title>
<para>
2001-02-09 19:54:47 +00:00
Installing Smarty is fairly straightforward, there is just one thing
you must be aware of. Smarty creates PHP scripts from the templates.
This usually means allowing user "nobody" (or whomever the web server
runs as) to have permission to write the files. Each installation of a
Smarty application minimally needs a templates directory and a compiled
templates directory. If you use configuration files you will also need
a directory for those. By default these are named "templates", and
"templates_c" and "configs" respectively. If you plan on using caching,
you will need to create a "cache" directory, also with permission to
write files.
2001-01-04 20:33:27 +00:00
</para>
2001-02-09 19:54:47 +00:00
<para>
TECHNICAL NOTE: You can get around the need to allow the web server
user write access to compile templates. Smarty needs to compile the
templates only once. This can be done from the command line, using the
CGI version of PHP. example: "php -q index.php". Once the templates are
compiled, they should run fine from the web environment. If you change
a template, you must recompile from the command line again. If you do
not have the CGI version of PHP available and you are concerned about
world-writable directory access, you can chmod 777 the compile_dir, let
the templates compile once as the web server user, then change the
directory mode back to 700. If you are using the caching feature of
Smarty, the cache directory must always have write access for the web
server user.
</para>
<para>
TECHNICAL NOTE: If you do not have access to the php.ini file, you can
change non-server settings (such as your include_path) with the
ini_set() command (available in PHP 4.0.4 or later.) example:
ini_set("include_path",".:/usr/local/lib/php");
</para>
2001-01-04 20:33:27 +00:00
<para>
2001-01-18 20:41:43 +00:00
Copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php
scripts to a directory that is in your PHP include_path. NOTE: PHP will
try to create a directory alongside the executing script called
"templates_c". Be sure that directory permissions allow this to happen.
You will see PHP error messages if this fails. You can also create the
directory yourself before hand, and change the file ownership
accordingly. See below.
2001-01-04 20:33:27 +00:00
</para>
<example>
<title>Example of installing Smarty</title>
<programlisting>
# be sure you are in the web server document tree
# this assumes your web server runs as user "nobody"
2001-01-29 16:36:07 +00:00
# and you are in a un*x environment
2001-02-09 19:54:47 +00:00
gtar -zxvf Smarty-[version].tar.gz
2001-01-12 20:39:27 +00:00
mkdir templates_c
2001-01-04 20:33:27 +00:00
chown nobody:nobody templates_c
2001-02-01 17:40:59 +00:00
chmod 700 templates_c
2001-02-09 19:54:47 +00:00
# if you are using caching, do the following
2001-02-01 17:40:59 +00:00
mkdir cache
chown nobody:nobody cache
chmod 700 cache
2001-01-04 20:33:27 +00:00
</programlisting>
</example>
<para>
Next, try running the index.php script from your web browser.
</para>
</sect1>
</chapter>
<chapter>
<title>Setting up Smarty</title>
<para>
2001-02-09 19:54:47 +00:00
There are several variables that are at the top of the Smarty.class.php
file. The default settings work for all of the examples and tutorials.
2001-01-04 20:33:27 +00:00
</para>
<sect1>
<title>Configuration variables</title>
<para></para>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.template.dir">
<title>$template_dir</title>
<para>
This is the name of the directory where template files are located.
By default this is "./templates".
</para>
2001-02-09 19:54:47 +00:00
<para>
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
</para>
2001-02-07 23:09:32 +00:00
</sect2>
<sect2 id="setting.compile.dir">
<title>$compile_dir</title>
<para>
This is the name of the directory where compiled templates are
located. By default this is "./templates_c". This was
added to Smarty version 1.2.1.
</para>
2001-02-09 19:54:47 +00:00
<para>
TECHNICAL NOTE: This setting must be either a relative or
absolute path. include_path is not used for writing files.
</para>
<para>
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
</para>
2001-02-07 23:09:32 +00:00
</sect2>
<sect2 id="setting.config.dir">
<title>$config_dir</title>
<para>
This is the directory used to store config files used in the templates.
2001-02-09 19:54:47 +00:00
Default is "./configs".
</para>
<para>
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
2001-02-07 23:09:32 +00:00
</para>
</sect2>
<sect2 id="setting.global.assign">
<title>$global_assign</title>
<para>
2001-02-09 19:54:47 +00:00
This is a list of variables that are always implicitly assigned
to the template engine. This is handy for making global
variables or server variables available to all templates
without having to manually assign them. $SCRIPT_NAME is
globally assigned by default.
2001-02-07 23:09:32 +00:00
</para>
</sect2>
2001-01-04 20:33:27 +00:00
<sect2>
<title>$compile_check</title>
<para>
2001-01-12 20:39:27 +00:00
Upon each invocation of the PHP application, Smarty recursively
traverses the template directory and its subdirectories and
2001-02-01 17:40:59 +00:00
searches for templates that have changed (later time stamp)
since the last time they were compiled. For each one that has
changed, it recompiles that template. By default this variable
is set to true. Once an application is put into production and
it is initially compiled, the compile_check step is no longer
needed. Be sure to set $compile_check to "false" to improve
performance! Note that if you change this to "false" and a
template file is changed, you will *not* see the change since
the template will not get recompiled. See <link
linkend="setting.force.compile">$force_compile</link>
</para>
</sect2>
<sect2 id="setting.force.compile">
<title>$force_compile</title>
<para>
This forces Smarty to compile all templates on every
invocation. This setting overrides $compile_check. By default
this is disabled. This is handy for development and debugging.
It should never be used in a production environment.
</para>
</sect2>
<sect2 id="setting.caching">
<title>$caching</title>
<para>
2001-02-09 19:54:47 +00:00
This tells Smarty whether or not to cache the output of the
templates. By default this is set to false. If your templates
generate the same content over and over, it is advisable to
turn on caching. This will result significant performance
gains. You can also have multiple caches for the same template.
See <link linkend="api.is.cached">is_cached</link> for details.
This was added to Smarty 1.3.0.
2001-02-01 17:40:59 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.cache.dir">
<title>$cache_dir</title>
<para>
This is the name of the directory where template caches are
located. By default this is "./cache". This was
added to Smarty version 1.3.0.
</para>
2001-02-09 19:54:47 +00:00
<para>
TECHNICAL NOTE: This setting must be either a relative or
absolute path. include_path is not used for writing files.
</para>
<para>
TECHNICAL NOTE: It is not mandatory to put this directory under
the web server document root.
</para>
2001-02-07 23:09:32 +00:00
</sect2>
2001-02-01 17:40:59 +00:00
<sect2 id="setting.cache.lifetime">
<title>$cache_lifetime</title>
<para>
This is the length of time in seconds that a template cache is
valid. Once this time has expired, the cache will be
regenerated. $caching must be set to "true" for this setting to
work. You can also force the cache to expire with <link
linkend="api.clear.all.cache">clear_all_cache</link>. This was
added to Smarty 1.3.0.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.tpl.file.ext">
2001-01-04 20:33:27 +00:00
<title>$tpl_file_ext</title>
<para>
2001-01-18 20:41:43 +00:00
This is the extention used for template files. By default this
is ".tpl". All other files in the template directory are
ignored.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.allow.php">
2001-01-04 20:33:27 +00:00
<title>$allow_php</title>
<para>
2001-01-12 20:39:27 +00:00
Whether or not to allow PHP code in the templates. If set to
false, PHP code is escaped and not interpreted. Embedding PHP
2001-01-29 16:36:07 +00:00
code into templates is highly discouraged. Use <link
linkend="custom.functions">custom functions</link> or <link
linkend="variable.modifiers">modifiers</link> instead. Default
is "false".
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-21 18:04:26 +00:00
<sect2 id="setting.php.handling">
<title>$php_handling</title>
<para>
This tells Smarty how to handle PHP code embedded in the
tempalates. There are four possible settings, default being
SMARTY_PHP_PASSTHRU.
</para>
<itemizedlist>
<listitem><para>SMARTY_PHP_PASSTHRU - Smarty echos tags as-is.</para></listitem>
<listitem><para>SMARTY_PHP_QUOTE - Smarty quotes the tags as
html entities.</para></listitem>
<listitem><para>SMARTY_PHP_REMOVE - Smarty removes the tags from
the templates.</para></listitem>
<listitem><para>SMARTY_PHP_ALLOW - Smarty will execute the tags
as PHP code.</para></listitem>
</itemizedlist>
<para>
NOTE: Embedding PHP code into templates is highly discouraged.
Use <link linkend="custom.functions">custom functions</link> or
<link linkend="variable.modifiers">modifiers</link> instead.
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.left.delimiter">
2001-01-04 20:33:27 +00:00
<title>$left_delimiter</title>
<para>
2001-01-12 20:39:27 +00:00
This is the left delimiter used by the template language. Default is "{".
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.right.delimiter">
2001-01-04 20:33:27 +00:00
<title>$right_delimiter</title>
<para>
2001-01-12 20:39:27 +00:00
This is the right delimiter used by the template language. Default is "}".
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.custom.funcs">
2001-01-04 20:33:27 +00:00
<title>$custom_funcs</title>
<para>
2001-01-29 16:36:07 +00:00
This is a mapping of the names of <link
2001-02-09 19:54:47 +00:00
linkend="custom.functions">custom functions</link> in the
template to the names of functions in PHP. The default custom
functions that come bundled with Smarty are located in
Smarty.addons.php.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="setting.custom.mods">
2001-01-04 20:33:27 +00:00
<title>$custom_mods</title>
<para>
2001-01-18 20:41:43 +00:00
This is a mapping of the names of variable
<link linkend="variable.modifiers">modifiers</link> in the template to
2001-02-09 19:54:47 +00:00
the names of functions in PHP. The default variable modifiers
that come bundled with Smarty are located in
Smarty.addons.php.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
</sect1>
</chapter>
<chapter>
<title>Smarty API</title>
<para>
These functions are used in the PHP portion of your application.
</para>
<sect1>
<title>Smarty API Functions</title>
2001-02-07 23:09:32 +00:00
<sect2 id="api.assign">
2001-01-04 20:33:27 +00:00
<title>assign</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>assign</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>assign</function></funcdef>
<paramdef>string <parameter>varname</parameter></paramdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
2001-02-08 14:18:25 +00:00
This is used to assign values to the templates. You can
explicitly pass name/value pairs, or associative arrays
containing the name/value pairs.
2001-01-04 20:33:27 +00:00
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>assign</title>
<programlisting>
// passing name/value pairs
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
// passing an associative array
$smarty->assign(array("city" => "Lincoln","state" => "Nebraska"));
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.append">
2001-01-04 20:33:27 +00:00
<title>append</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>append</function></funcdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>append</function></funcdef>
<paramdef>string <parameter>varname</parameter></paramdef>
<paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
2001-02-08 14:18:25 +00:00
This is used to append data to variables in the template. You
can explicitly pass name/value pairs, or associative arrays
containing the name/value pairs.
2001-01-04 20:33:27 +00:00
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>append</title>
<programlisting>
// passing name/value pairs
$smarty->append("Name","Fred");
$smarty->append("Address",$address);
// passing an associative array
$smarty->append(array("city" => "Lincoln","state" => "Nebraska"));
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.clear.assign">
2001-01-04 20:33:27 +00:00
<title>clear_assign</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>clear_assign</function></funcdef>
<paramdef>string <parameter>var</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
2001-02-08 14:18:25 +00:00
This clears the value of an assigned variable. This
can be a single value, or an array of values. Array
functionality was added to Smarty 1.3.0.
2001-01-04 20:33:27 +00:00
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>clear_assign</title>
<programlisting>
// clear a single variable
$smarty->clear_assign("Name");
// clear multiple variables
$smarty->clear_assign(array("Name","Address","Zip"));
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.clear.all.assign">
2001-01-04 20:33:27 +00:00
<title>clear_all_assign</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>clear_all_assign</function></funcdef>
<paramdef><parameter></parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This clears the values of all assigned variables.
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>clear_all_assign</title>
<programlisting>
// clear all assigned variables
$smarty->clear_all_assign();
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.register.function">
<title>register_function</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>register_function</function></funcdef>
<paramdef>string <parameter>funcname</parameter></paramdef>
<paramdef>string <parameter>funcimpl</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Use this to dynamically register functions with Smarty. Pass in
the Smarty function name, followed by the actual function name
that it is mapped to.
</para>
<example>
<title>register_function</title>
<programlisting>
$smarty->register_function("date_now","print_current_date");
function print_current_date ($params) {
extract($params);
if(empty($format))
$format="%b %e, %Y";
echo strftime($format,time());
}
// now you can use this in Smarty to print the current date: {date_now}
// or, {date_now format="%Y/%m/%d"} to format it.
</programlisting>
</example>
</sect2>
<sect2 id="api.register.modifier">
<title>register_modifier</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>register_modifier</function></funcdef>
<paramdef>string <parameter>modname</parameter></paramdef>
<paramdef>string <parameter>funcimpl</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
Use this to dynamically register modifiers with Smarty. Pass in
the Smarty modifier name, followed by the actual function name
that it is mapped to.
</para>
<example>
<title>register_modifier</title>
<programlisting>
// let's map PHP's stripslashes function to a Smarty modifier.
$smarty->register_modifier("sslash","stripslashes");
// now you can use {$var|sslash} to strip slashes from variables
</programlisting>
</example>
</sect2>
<sect2 id="api.clear.cache">
<title>clear_cache</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>clear_cache</function></funcdef>
<paramdef>string <parameter>template</parameter></paramdef>
<paramdef>string <parameter>cache id</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
2001-02-08 14:48:51 +00:00
This clears the cache for the specified template. If you have
2001-02-07 23:09:32 +00:00
multiple caches for this template, you can clear a specific
cache by supplying the cache id as the second parameter. See the
<link linkend="section.caching">caching section</link> for more
information. This was added to Smarty 1.3.0.
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>clear_cache</title>
<programlisting>
// clear the cache for a template
$smarty->clear_cache("index.tpl");
// clear the cache for a particular cache id in an multiple-cache template
$smarty->clear_cache("index.tpl","CACHEID");
</programlisting>
</example>
2001-02-07 23:09:32 +00:00
</sect2>
2001-02-01 17:40:59 +00:00
<sect2 id="api.clear.all.cache">
<title>clear_all_cache</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>clear_all_cache</function></funcdef>
<paramdef><parameter></parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This clears the entire template cache. This was added to Smarty
1.3.0.
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>clear_all_cache</title>
<programlisting>
// clear the entire cache
$smarty->clear_all_cache();
</programlisting>
</example>
2001-02-01 17:40:59 +00:00
</sect2>
2001-02-05 21:10:20 +00:00
<sect2 id="api.is.cached">
<title>is_cached</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>is_cached</function></funcdef>
2001-02-09 19:54:47 +00:00
<paramdef>string <parameter>template</parameter></paramdef>
2001-02-21 19:54:15 +00:00
<paramdef>[string <parameter>cache_id</parameter>]</paramdef>
2001-02-05 21:10:20 +00:00
</funcprototype>
</funcsynopsis>
<para>
This returns true if there is a valid cache for this template.
2001-02-08 14:18:25 +00:00
This only works if <link
linkend="setting.caching">caching</link> is set to true. This
was added to Smarty 1.3.0.
2001-02-05 21:10:20 +00:00
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>is_cached</title>
<programlisting>
$smarty->caching = true;
2001-02-08 14:48:51 +00:00
if(!$smarty->is_cached("index.tpl")) {
2001-02-08 14:18:25 +00:00
// do database calls, assign vars here
}
$smarty->display("index.tpl");
2001-02-08 14:48:51 +00:00
</programlisting>
</example>
2001-02-09 19:54:47 +00:00
<para>
You can also pass a cache id as an an optional second parameter
in the case you want multiple caches for the given template.
</para>
2001-02-08 14:48:51 +00:00
<example>
<title>is_cached with multiple-cache template</title>
<programlisting>
$smarty->caching = true;
2001-02-21 19:54:15 +00:00
if(!$smarty->is_cached("index.tpl","FrontPage")) {
2001-02-08 14:48:51 +00:00
// do database calls, assign vars here
}
2001-02-21 19:54:15 +00:00
$smarty->display("index.tpl","FrontPage");
2001-02-08 14:48:51 +00:00
2001-02-08 14:18:25 +00:00
</programlisting>
</example>
2001-02-05 21:10:20 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.get.template.vars">
2001-01-04 20:33:27 +00:00
<title>get_template_vars</title>
<funcsynopsis>
<funcprototype>
<funcdef>array <function>get_template_vars</function></funcdef>
<paramdef><parameter></parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This gets an array of the currently assigned template vars.
</para>
2001-02-08 14:18:25 +00:00
<example>
<title>get_template_vars</title>
<programlisting>
// get all assigned template vars
$tpl_vars = $smarty->get_template_vars();
// take a look at them
var_dump($tpl_vars);
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.display">
2001-01-04 20:33:27 +00:00
<title>display</title>
<funcsynopsis>
<funcprototype>
<funcdef>void <function>display</function></funcdef>
<paramdef>string <parameter>template</parameter></paramdef>
2001-02-07 23:09:32 +00:00
<paramdef>string <parameter>cache id</parameter></paramdef>
2001-01-04 20:33:27 +00:00
</funcprototype>
</funcsynopsis>
<para>
2001-01-29 16:36:07 +00:00
This displays the template. Supply a path relative to the
2001-02-07 23:09:32 +00:00
<link linkend="setting.template.dir">template directory</link>.
As an optional second parameter, you can pass a cache id.
See the <link linkend="section.caching">caching section</link> for
more information.
2001-01-04 20:33:27 +00:00
</para>
2001-02-08 14:18:25 +00:00
<example>
2001-02-08 14:48:51 +00:00
<title>display</title>
2001-02-08 14:18:25 +00:00
<programlisting>
include("Smarty.class.php");
$smarty = new Smarty;
// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl"))
{
// dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" = > "68502"
);
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data);
}
// display the output
$smarty->display("index.tpl");
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-02-07 23:09:32 +00:00
<sect2 id="api.fetch">
2001-01-04 20:33:27 +00:00
<title>fetch</title>
<funcsynopsis>
<funcprototype>
<funcdef>string <function>fetch</function></funcdef>
<paramdef>string <parameter>template</parameter></paramdef>
2001-02-21 19:54:15 +00:00
<paramdef>[string <parameter>cache_id</parameter>]</paramdef>
2001-01-04 20:33:27 +00:00
</funcprototype>
</funcsynopsis>
<para>
2001-02-09 19:54:47 +00:00
This returns the template output instead of displaying it.
Supply a path relative to the <link
linkend="setting.template.dir">template directory</link>
2001-01-04 20:33:27 +00:00
</para>
2001-02-07 23:09:32 +00:00
<example>
2001-02-08 14:48:51 +00:00
<title>fetch</title>
2001-01-04 20:33:27 +00:00
<programlisting>
include("Smarty.class.php");
$smarty = new Smarty;
2001-02-05 21:10:20 +00:00
// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl"))
{
// dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" = > "68502"
);
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data);
}
2001-01-04 20:33:27 +00:00
2001-02-08 14:18:25 +00:00
// capture the output
$output = $smarty->fetch("index.tpl");
// do something with $output here
echo $output;
2001-01-04 20:33:27 +00:00
</programlisting>
2001-02-07 23:09:32 +00:00
</example>
2001-01-04 20:33:27 +00:00
</sect2>
</sect1>
</chapter>
<chapter>
<title>Smarty Templates</title>
<para>
The templates are the heart of Smarty. These are the files that the designers
work with. They're basically pages made up of static content interspersed with
template markup tags. These tags are placeholders for variables or blocks of logic.
</para>
<sect1>
<title>Syntax</title>
<para>
For these examples, we will assume that you are using the default
template tag delimiters, which are "{" and "}". In Smarty, all content
outside of delimiter tags is displayed as static content, or unchanged.
When Smarty encounters template tags {}, it attempts to interpret what is
between the tags, and displays the appropriate output in place of them.
</para>
<sect2>
<title>Variables</title>
<para>
2001-01-04 21:39:51 +00:00
There are three basic types of variables in Smarty, each with their
2001-01-04 20:33:27 +00:00
own unique syntax.
</para>
<sect3>
<title>Variables assigned from PHP</title>
<para>
Variables that are assigned from PHP are displayed by preceeding
them with a dollar sign ($) and enclosing the variable in delimiters
like so: {$varname}
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>displaying assigned variables</title>
2001-01-04 20:33:27 +00:00
<programlisting>
Hello {$firstname}, glad to see you could make it.
<p>
2001-01-12 22:14:51 +00:00
Your last login was on {$lastLoginDate}.
OUTPUT:
Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.
2001-01-04 20:33:27 +00:00
</programlisting>
</example>
2001-01-26 17:47:59 +00:00
</sect3>
<sect3>
<title>Associative arrays</title>
2001-01-04 20:33:27 +00:00
<para>
2001-01-26 17:47:59 +00:00
You can also print variables that are assigned as associative
arrays from PHP by supplying the key value with the array name.
2001-01-04 20:33:27 +00:00
</para>
2001-01-26 17:47:59 +00:00
<example>
<title>displaying assigned associative array variables</title>
<programlisting>
{$Contacts.fax}<br>
{$Contacts.email}<br>
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br>
{$Contacts.phone.cell}<br>
OUTPUT:
555-222-9876<br>
zaphod@slartibartfast.com<br>
555-444-3333<br>
555-111-1234<br>
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect3>
2001-01-26 17:47:59 +00:00
2001-01-04 20:33:27 +00:00
<sect3>
<title>Variables passed from config files</title>
<para>
Variables that are passed in from config files are displayed by enclosing
them with hash marks (#) and enclosing the variable in delimiters
like so: {#varname#}
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>displaying config variables</title>
2001-01-04 20:33:27 +00:00
<programlisting>
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
</programlisting>
</example>
<para>
Config file variables cannot be displayed until
after they are loaded in from a config file. This procedure is
2001-01-12 22:14:51 +00:00
explained later in this document under
2001-01-18 20:41:43 +00:00
<link linkend="builtin.functions.configload">config_load</link>.
2001-01-04 20:33:27 +00:00
</para>
</sect3>
<sect3>
<title>Variables internal to template</title>
<para>
Variables that are internal to the templates are displayed by enclosing
them with percent signs (%) and enclosing the variable in delimiters
2001-01-12 22:14:51 +00:00
like so: {%varname%} So far, section properties are the only internal
variables used in Smarty, which can be found later in this document under
<link linkend="builtin.functions.section">section</link>.
2001-01-04 20:33:27 +00:00
</para>
</sect3>
</sect2>
<sect2>
<title>Functions</title>
<para>
Functions are processed and displayed by enclosing the function and its
attributes into delimiters like so: {funcname attr1="val" attr2="val"}
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function syntax</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{config_load file="colors.conf"}
{include file="header.tpl"}
{if $name eq "Fred"}
You are not allowed here
{else}
Welcome, <font color="{#fontColor#}">{$name}!</font>
{/if}
{include file="footer.tpl"}
</programlisting>
</example>
<para>
2001-01-29 16:36:07 +00:00
Both built-in functions and custom functions have the same syntax
in the templates. Built-in functions are the inner workings of
Smarty, such as {if}, {section} and {strip}. They cannot be
modified. Custom functions are located in the Smarty.addons.class
file. They can be modified to your liking, or add new ones.
{html_options} and {html_select_date} are examples of custom
functions.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
<sect2>
<title>Attributes</title>
<para>
Attributes to functions are much like HTML attributes. Static
2001-01-12 22:14:51 +00:00
values don't have to be enclosed in quotes, but it is recommended
for literal strings. If not quoted, you may use a syntax that Smarty may confuse
2001-01-04 21:39:51 +00:00
with another function, such as a boolean value. Variables may
2001-01-19 14:37:13 +00:00
also be used, and should not be in quotes.
2001-01-04 20:33:27 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function attribute syntax</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
2001-01-18 20:41:43 +00:00
<SELECT name=company>
2001-01-04 20:33:27 +00:00
{html_options values=$vals selected=$selected output=$output}
2001-01-18 20:41:43 +00:00
</SELECT>
2001-01-04 20:33:27 +00:00
</programlisting>
</example>
</sect2>
<sect2>
<title>Comments</title>
<para>
Template comments are surrounded by asterisks, and that is surrounded
by the delimiter tags like so: {* this is a comment *}
Smarty comments are not displayed in the final output of the template.
They are used mainly for making the templates more understandable.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>Comments</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{* Smarty *}
{* include the header file here *}
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{* display dropdown lists *}
2001-01-18 20:41:43 +00:00
<SELECT name=company>
2001-01-04 20:33:27 +00:00
{html_options values=$vals selected=$selected output=$output}
2001-01-18 20:41:43 +00:00
</SELECT>
2001-01-04 20:33:27 +00:00
</programlisting>
</example>
</sect2>
</sect1>
2001-01-12 22:14:51 +00:00
<sect1 id="configfiles">
2001-01-04 20:33:27 +00:00
<title>Config Files</title>
<para>
2001-02-09 19:54:47 +00:00
Config files are handy for designers to manage global template
variables from one file. One example is template colors. Normally if
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
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
Config_File.class.php in your PHP include path. Config_File.class.php
comes bundled with Smarty. Smarty will implicitly include the file if
you don't already include it in your application.
2001-01-04 20:33:27 +00:00
</para>
<example>
<title>Example of config file syntax</title>
<programlisting>
# global variables
pageTitle = "Main Menu"
bodyBgColor = #000000
tableBgColor = #000000
rowBgColor = #00ff00
2001-01-04 21:39:51 +00:00
[Customer]
2001-01-04 20:33:27 +00:00
pageTitle = "Customer Info"
2001-01-04 21:39:51 +00:00
[Login]
2001-01-04 20:33:27 +00:00
pageTitle = "Login"
focus = "username"
Intro = """This is a value that spans more
than one line. you must enclose
it in triple quotes."""
</programlisting>
</example>
<para>
2001-01-25 16:22:44 +00:00
Values of config file variables can be in quotes, but not necessary.
2001-01-04 20:33:27 +00:00
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
("""). You can put comments into config files by any syntax that is
not a valid config file syntax. We recommend using a hashmark (#) at the
beginning of the line.
</para>
<para>
This config file example has two sections. Section names are enclosed
in brackets []. The four variables at the top are global
variables, or variables not within a section. These variables are
always loaded from the config file.
If a particular section is loaded, then the global variables and the
variables from that section are loaded. If a variable exists both as
a global and in a section, the section variable is used. If you name two
variables the same within a section, the last one will be used.
</para>
<para>
Config files are loaded into templates with the built-in function
2001-01-18 20:41:43 +00:00
called <link linkend="builtin.functions.configload">config_load</link>.
2001-01-04 20:33:27 +00:00
</para>
</sect1>
2001-01-12 22:14:51 +00:00
<sect1 id="builtin.functions">
2001-01-04 20:33:27 +00:00
<title>Built-in Functions</title>
<para>
Smarty comes with several built-in functions. Built-in functions
are integral to the template language. You cannot create custom
functions with the same names, nor can you modify built-in functions.
</para>
2001-01-18 20:41:43 +00:00
<sect2 id="builtin.functions.configload">
2001-01-04 20:33:27 +00:00
<title>config_load</title>
<para>
This function is used for loading in variables from a
2001-01-12 22:14:51 +00:00
configuration file into the template. You must have the Config_file.class.php
file somewhere in your PHP include path for config_load to work properly.
See <link linkend="configfiles">Config Files</link> for more info.
2001-01-04 20:33:27 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function config_load</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{config_load file="colors.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
</programlisting>
</example>
<para>
2001-02-09 19:54:47 +00:00
Config files may also contain sections. You can load variables from
within a section with the added attribute
<emphasis>section</emphasis>.
2001-01-04 20:33:27 +00:00
</para>
2001-02-09 19:54:47 +00:00
<para>
NOTE: <emphasis>Config file sections</emphasis> and the built-in
template function called <emphasis>section</emphasis> have nothing
to do with each other, they just happen to share a common naming
convention.
</para>
2001-01-04 20:33:27 +00:00
<example>
2001-01-26 17:47:59 +00:00
<title>function config_load with section</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{config_load file="colors.conf" section="Customer"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
</programlisting>
</example>
</sect2>
<sect2>
<title>include</title>
<para>
2001-02-09 19:54:47 +00:00
Include tags are used for including other templates in the current
template. Any variables available in the current template are also
available within the included template. The include tag must have
the attribute "file", which contains the path to the included
template file relative to the template directory.
2001-01-04 20:33:27 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function include</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}
</programlisting>
</example>
<para>
2001-02-09 19:54:47 +00:00
You can also pass variables to included templates as attributes.
Any variables explicitly passed to an included template as
attributes are only available within the scope of the included
file. Attribute variables override current template variables, in
the case they are named alike.
2001-01-04 20:33:27 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function include passing variables</title>
2001-01-04 20:33:27 +00:00
<programlisting>
2001-01-12 22:14:51 +00:00
{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}
2001-01-04 20:33:27 +00:00
{* body of template goes here *}
{include file="footer.tpl" logo="http://my.domain.com/logo.gif"}
</programlisting>
</example>
</sect2>
2001-02-01 17:40:59 +00:00
<sect2 id="function.insert">
2001-01-04 20:33:27 +00:00
<title>insert</title>
<para>
The insert tag in Smarty serves a special purpose. You may
run into the situation where it is impossible to pass data to a template
before the template is executed because there is info in the template
needed to aquire the data, kind of a catch 22. The insert tag is a way
2001-02-01 20:08:04 +00:00
to callback a function in PHP during runtime of the template.
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-01-12 22:14:51 +00:00
Let's say you have a template with a banner slot at the top of the page. The
banner can contain any mixture of HTML, images, flash, etc. so we can't just
use a static link here. In comes the insert tag: the template
knows #banner_location_id# and #site_id# values (gathered from a config file),
and needs to call a function to get the banner's contents.
2001-01-04 20:33:27 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>function insert</title>
2001-01-04 20:33:27 +00:00
<programlisting>
{* example of fetching a banner *}
2001-01-12 22:14:51 +00:00
{insert name="getBanner" lid=#banner_location_id# sid=#site_id#}
2001-01-04 20:33:27 +00:00
</programlisting>
</example>
<para>
2001-01-12 22:14:51 +00:00
In this example, we are using the name "getBanner" and passing the
parameters #banner_location_id# and #site_id#. Smarty will look
2001-01-04 20:33:27 +00:00
for a function named insert_getBanner() in your PHP application, passing
2001-01-12 22:14:51 +00:00
the values of #banner_location_id# and #site_id# as the first argument
2001-01-21 22:17:28 +00:00
in an associative array. All insert function names in
2001-01-12 22:14:51 +00:00
your application must be prepended with "insert_" to remedy possible
2001-01-18 20:41:43 +00:00
function name-space conflicts. Your insert_getBanner() function should
2001-01-04 20:33:27 +00:00
do something with the passed values and return the results. These results
2001-01-12 22:14:51 +00:00
are then displayed in the template in place of the insert tag.
In this example, Smarty would call this function:
2001-01-04 20:33:27 +00:00
insert_getBanner(array("banner_id" => "12345","page_id" => "67890"));
2001-01-12 22:14:51 +00:00
and display the returned results in place of the insert tag.
2001-01-04 20:33:27 +00:00
</para>
<para>
2001-02-07 23:09:32 +00:00
TECHNICAL NOTE: It is possible to have portions of the template not
cached. If you have <link linkend="section.caching">caching</link>
turned on, insert tags will not be cached. They will run
dynamically every time the page is created, even within cached
pages. This works good for things like banners, polls, live
weather, search results, user feedback areas, etc.
2001-01-04 20:33:27 +00:00
</para>
</sect2>
<sect2>
<title>if,elseif,else</title>
2001-01-04 21:39:51 +00:00
<para>
2001-02-01 20:08:04 +00:00
if statements in Smarty have much the same flexibility as php if
statements, with a few added features for the template engine.
Every <emphasis>if</emphasis> must be paired with an
<emphasis>/if</emphasis>. <emphasis>else</emphasis> and
<emphasis>elseif</emphasis> are also permitted. "eq", "ne","neq",
"gt", "lt", "lte", "le", "gte" "ge","is even","is odd", "is not
even","is not odd","not","mod","div by","even by","odd
by","==","!=",">", "<","<=",">=" are all valid conditional
2001-02-19 19:10:58 +00:00
qualifiers, and must be separated from surrounding elements by
spaces.
2001-01-04 21:39:51 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>if statements</title>
2001-01-04 21:39:51 +00:00
<programlisting>
{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
{* an example with "or" logic *}
{if $name eq "Fred" or $name eq "Wilma"}
...
{/if}
2001-02-01 20:08:04 +00:00
{* same as above *}
{if $name == "Fred" || $name == "Wilma"}
...
{/if}
2001-02-19 19:10:58 +00:00
{* the following syntax will NOT work, conditional qualifiers
must be separated from surrounding elements by spaces *}
{if $name=="Fred" || $name=="Wilma"}
...
{/if}
2001-02-01 20:08:04 +00:00
2001-01-04 21:39:51 +00:00
{* parenthesis are allowed *}
2001-01-09 17:11:32 +00:00
{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
2001-01-04 21:39:51 +00:00
...
{/if}
2001-01-18 20:41:43 +00:00
{* you can also imbed php function calls *}
2001-01-05 14:40:26 +00:00
{if count($var) gt 0}
2001-01-04 21:39:51 +00:00
...
{/if}
{* test if values are even or odd *}
{if $var is even}
...
{/if}
{if $var is odd}
...
{/if}
{if $var is not odd}
...
{/if}
2001-01-25 16:22:44 +00:00
{* test if var is divisible by 4 *}
2001-01-12 21:02:55 +00:00
{if $var is div by 4}
...
{/if}
2001-01-04 21:39:51 +00:00
{* test if var is even, grouped by two. i.e.,
2001-01-12 21:02:55 +00:00
0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *}
2001-01-04 21:39:51 +00:00
{if $var is even by 2}
...
{/if}
2001-01-12 21:02:55 +00:00
{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}
{if $var is even by 3}
...
{/if}
2001-01-04 21:39:51 +00:00
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>ldelim,rdelim</title>
2001-01-04 21:39:51 +00:00
<para>
ldelim and rdelim are used for displaying the literal delimiter, in
our case "{" or "}". The template engine always tries to interpret
delimiters, so this is the way around that.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>ldelim, rdelim</title>
2001-01-04 21:39:51 +00:00
<programlisting>
{* this will print literal delimiters out of the template *}
{ldelim}funcname{rdelim} is how functions look in Smarty!
2001-01-09 17:11:32 +00:00
OUTPUT:
{funcname} is how functions look in Smarty!
2001-01-04 21:39:51 +00:00
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>literal</title>
2001-01-04 21:39:51 +00:00
<para>
Literal tags allow a block of data to be taken literally,
not being interpreted by the Smarty engine. This is handy
for things like javascript sections, where there maybe
curly braces and such things that would confuse the template
parser. Anything within {literal}{/literal} tags is not
interpreted, but displayed as-is.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>literal tags</title>
2001-01-04 21:39:51 +00:00
<programlisting>
{literal}
<script language=javascript>
<!--
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
// -->
</script>
{/literal}
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-01-18 20:41:43 +00:00
<sect2 id="builtin.functions.section">
2001-01-04 20:33:27 +00:00
<title>section,sectionelse</title>
2001-01-09 17:11:32 +00:00
<para>
2001-02-09 19:54:47 +00:00
Template sections are used for looping over arrays of data. All
<emphasis>section</emphasis> tags must be paired with
<emphasis>/section</emphasis> tags. Required parameters are
<emphasis>name</emphasis> and <emphasis>loop</emphasis>. The name
of the section can be anything you like, made up of letters,
numbers and underscores. Sections can be nested, and the nested
section names must be unique from each other. The loop variable
(usually an array of values) determines the number of times the
section will loop. When printing a variable within a section, the
section name must be prepended to the variable name, separated by a
slash (/). If you are using an associative array, separate the key
and value with a period (.). <emphasis>sectionelse</emphasis> is
executed when there are no values in the loop variable.
2001-01-09 17:11:32 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>section</title>
2001-01-09 17:11:32 +00:00
<programlisting>
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$customer/custid}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
</programlisting>
</example>
<example>
2001-01-26 17:47:59 +00:00
<title>section loop variable</title>
2001-01-09 17:11:32 +00:00
<programlisting>
{* the loop variable only determines the number of times to loop.
you can access any variable from the template within the section.
This example assumes that $custid, $name and $address are all
arrays containing the same number of values *}
{section name=customer loop=$custid}
id: {$customer/custid}<br>
name: {$customer/name}<br>
address: {$customer/address}<br>
<p>
{/section}
OUTPUT:
id: 1000<br>
name: John Smith<br>
2001-02-21 19:54:15 +00:00
address: 253 N 45th<br>
2001-01-09 17:11:32 +00:00
<p>
id: 1001<br>
name: Jack Jones<br>
2001-02-21 19:54:15 +00:00
address: 417 Mulberry ln<br>
2001-01-09 17:11:32 +00:00
<p>
id: 1002<br>
2001-02-21 19:54:15 +00:00
name: Jane Munson<br>
address: 5605 apple st<br>
2001-01-09 17:11:32 +00:00
<p>
</programlisting>
</example>
<example>
2001-01-26 17:47:59 +00:00
<title>section names</title>
2001-01-09 17:11:32 +00:00
<programlisting>
{* the name of the section can be anything you like,
and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
id: {$mydata/custid}<br>
name: {$mydata/name}<br>
address: {$mydata/address}<br>
<p>
{/section}
</programlisting>
</example>
<example>
2001-01-26 17:47:59 +00:00
<title>nested sections</title>
2001-01-09 17:11:32 +00:00
<programlisting>
{* sections can be nested as deep as you like. With nested sections,
you can access complex data structures, such as multi-dimensional
arrays. In this example, $customer/contact_type is an array of
contact types for the current customer. *}
{section name=customer loop=$custid}
id: {$customer/custid}<br>
name: {$customer/name}<br>
address: {$customer/address}<br>
{section name=contact loop=$customer/contact_type}
{$customer/contact/contact_type}: {$customer/contact/contact_info}<br>
{/section}
<p>
{/section}
OUTPUT:
id: 1000<br>
name: John Smith<br>
2001-02-21 19:54:15 +00:00
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
2001-01-09 17:11:32 +00:00
<p>
id: 1001<br>
name: Jack Jones<br>
2001-02-21 19:54:15 +00:00
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
2001-01-09 17:11:32 +00:00
<p>
id: 1002<br>
2001-02-21 19:54:15 +00:00
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
2001-01-09 17:11:32 +00:00
<p>
</programlisting>
</example>
<example>
2001-01-26 17:47:59 +00:00
<title>sections and associative arrays</title>
<programlisting>
{* This is an example of printing an associative array
of data within a section *}
{section name=customer loop=$contacts}
2001-01-30 21:54:59 +00:00
name: {$customer/contacts.name}<br>
home: {$customer/contacts.home}<br>
cell: {$customer/contacts.cell}<br>
e-mail: {$customer/contacts.email}<p>
2001-01-26 17:47:59 +00:00
{/section}
OUTPUT:
name: John Smith<br>
2001-01-30 21:54:59 +00:00
home: 555-555-5555<br>
cell: 555-555-5555<br>
2001-01-26 17:47:59 +00:00
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
2001-01-30 21:54:59 +00:00
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
2001-01-26 17:47:59 +00:00
e-mail: jack@mydomain.com<p>
2001-01-30 21:54:59 +00:00
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
2001-01-26 17:47:59 +00:00
e-mail: jane@mydomain.com<p>
</programlisting>
</example>
<example>
<title>sectionelse</title>
2001-01-09 17:11:32 +00:00
<programlisting>
{* sectionelse will execute in the case there are no $custid values *}
{section name=customer loop=$custid}
id: {$customer/custid}<br>
{sectionelse}
there are no values in $custid.
{/section}
</programlisting>
</example>
2001-01-12 21:02:55 +00:00
<para>
Sections also have their own variables that handle section properties.
These are indicated by percent signs around the variable name, like so:
%sectionname.varname%
</para>
<sect3>
<title>index</title>
<para>
index is used to display the current loop iteration,
starting with zero.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>section property index</title>
2001-01-12 21:02:55 +00:00
<programlisting>
{section name=customer loop=$custid}
{%customer.index%} id: {$customer/custid}<br>
{/section}
OUTPUT:
0 id: 1000<br>
1 id: 1001<br>
2 id: 1002<br>
</programlisting>
</example>
</sect3>
<sect3>
<title>rownum</title>
<para>
rownum is used to display the current loop iteration,
starting with one.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>section property rownum</title>
2001-01-12 21:02:55 +00:00
<programlisting>
{section name=customer loop=$custid}
{%customer.rownum%} id: {$customer/custid}<br>
{/section}
OUTPUT:
1 id: 1000<br>
2 id: 1001<br>
3 id: 1002<br>
</programlisting>
</example>
</sect3>
<sect3>
<title>loop</title>
<para>
loop is used to display the total number
of iterations this section is looped. This can be used
inside or after the section.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>section property index</title>
2001-01-12 21:02:55 +00:00
<programlisting>
{section name=customer loop=$custid}
{%customer.index%} id: {$customer/custid}<br>
{/section}
There were {%customer.loop%} customers shown above.
OUTPUT:
0 id: 1000<br>
1 id: 1001<br>
2 id: 1002<br>
There were 3 customers shown above.
</programlisting>
</example>
</sect3>
<sect3>
<title>show</title>
<para>
2001-02-09 19:54:47 +00:00
<emphasis>show</emphasis> is used as a parameter to section.
<emphasis>show</emphasis> is a boolean value, true or false. If
false, the section will not be displayed. If there is a sectionelse
present, that will be alternately displayed.
2001-01-12 21:02:55 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>section property rownum</title>
2001-01-12 21:02:55 +00:00
<programlisting>
{* $show_customer_info may have been passed from the PHP
application, to regulate whether or not this section shows *}
{section name=customer loop=$custid show=$show_customer_info}
{%customer.rownum%} id: {$customer/custid}<br>
{/section}
{if %customer.show%}
the section was shown.
{else}
the section was not shown.
{/if}
OUTPUT:
1 id: 1000<br>
2 id: 1001<br>
3 id: 1002<br>
the section was shown.
</programlisting>
</example>
</sect3>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>strip</title>
2001-01-04 21:39:51 +00:00
<para>
2001-01-18 20:41:43 +00:00
Many times web designers
run into the issue where white space and carriage returns
2001-01-04 21:39:51 +00:00
affect the output of the rendered HTML (browser "features"), so you
must run all your tags together in the template to get the
desired results. This usually ends up in unreadable or
unmanagable templates.
</para>
<para>
Anything within {strip}{/strip} tags in Smarty are stripped of
2001-01-09 17:11:32 +00:00
the extra spaces or carriage returns at the beginnings and
ends of the lines before they are displayed.
2001-01-04 21:39:51 +00:00
This way you can keep your templates readable, and not worry
about extra white space causing problems.
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>strip tags</title>
2001-01-04 21:39:51 +00:00
<programlisting>
2001-01-05 14:40:26 +00:00
{* the following will be all run into one line upon output *}
2001-01-04 21:39:51 +00:00
{strip}
2001-01-05 14:40:26 +00:00
<table border=0>
<tr>
<td>
<A HREF="{$url}">
<font color="red">This is a test</font>
</A>
</td>
</tr>
</table>
2001-01-04 21:39:51 +00:00
{/strip}
2001-01-09 17:11:32 +00:00
OUTPUT:
<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>
2001-01-04 21:39:51 +00:00
</programlisting>
</example>
2001-01-09 17:11:32 +00:00
<para>
Notice that in the above example, all the lines begin and end
with HTML tags. Be aware that all the lines are run together.
If you have plain text at the beginning or end of any line,
they will be run together, and may not be desired results.
</para>
2001-01-05 14:40:26 +00:00
</sect2>
2001-01-04 20:33:27 +00:00
</sect1>
2001-01-12 20:39:27 +00:00
<sect1 id="custom.functions">
2001-01-04 20:33:27 +00:00
<title>Custom Functions</title>
2001-01-05 14:40:26 +00:00
<para>
Custom functions in Smarty work much the same as the built-in functions
2001-02-10 21:05:46 +00:00
syntactically.
2001-01-05 14:40:26 +00:00
</para>
2001-01-04 20:33:27 +00:00
<sect2>
<title>html_options</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>values</entry>
<entry>array</entry>
2001-02-10 21:05:46 +00:00
<entry>Yes, unless using options attribute</entry>
2001-02-06 22:17:51 +00:00
<entry><emphasis>n/a</emphasis></entry>
<entry>an array of values for dropdown</entry>
</row>
<row>
<entry>output</entry>
<entry>array</entry>
2001-02-10 21:05:46 +00:00
<entry>Yes, unless using options attribute</entry>
2001-02-06 22:17:51 +00:00
<entry><emphasis>n/a</emphasis></entry>
<entry>an array of output for dropdown</entry>
</row>
<row>
<entry>selected</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>the selected array element</entry>
</row>
<row>
<entry>options</entry>
<entry>associative array</entry>
2001-02-10 21:05:46 +00:00
<entry>Yes, unless using values and output</entry>
2001-02-06 22:17:51 +00:00
<entry><emphasis>n/a</emphasis></entry>
<entry>an associative array of values and output</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-05 14:40:26 +00:00
<para>
html_options is a custom function that creates html option
lists with provided data. It takes care of which item is
2001-02-06 22:17:51 +00:00
selected by default as well. Required attributes are values
and output, unless you use options instead.
2001-01-05 14:40:26 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>html_options</title>
2001-01-05 14:40:26 +00:00
<programlisting>
{* assume that $cust_ids, and $cust_names are arrays of values,
and $customer_id may or may not be set to a value *}
<select name=customer_id>
{html_options values=$cust_ids selected=$customer_id output=$cust_names}
</select>
2001-01-31 23:06:18 +00:00
{* an alternative method is to pass the values & output as an associative array into
options. $customer_options is an associative array in this example. This
functionality was added to Smarty 1.3.0 *}
<select name=customer_id>
{html_options options=$customer_options selected=$customer_id}
</select>
2001-01-09 17:11:32 +00:00
OUTPUT:
<select name=customer_id>
<option value="1000">Joe Schmoe<option>
<option value="1001" selected>Jack Smith<option>
<option value="1002">Jane Johnson<option>
<option value="1003">Charlie Brown<option>
</select>
2001-01-05 14:40:26 +00:00
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>html_select_date</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>prefix</entry>
<entry>string</entry>
<entry>No</entry>
<entry>Date_</entry>
<entry>what to prefix the var name with</entry>
</row>
<row>
<entry>time</entry>
<entry>timestamp</entry>
<entry>No</entry>
<entry>current time</entry>
<entry>what date/time to use</entry>
</row>
<row>
<entry>start_year</entry>
<entry>string</entry>
<entry>No</entry>
<entry>current year</entry>
<entry>the first year in the dropdown</entry>
</row>
<row>
<entry>end_year</entry>
<entry>string</entry>
<entry>No</entry>
<entry>same as start_year</entry>
<entry>the last year in the dropdown</entry>
</row>
<row>
<entry>display_days</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>true</entry>
<entry>whether to display days or not</entry>
</row>
<row>
<entry>display_months</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>true</entry>
<entry>whether to display months or not</entry>
</row>
<row>
<entry>display_years</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>true</entry>
<entry>whether to display years or not</entry>
</row>
<row>
<entry>month_format</entry>
<entry>string</entry>
<entry>No</entry>
<entry>%B</entry>
<entry>what format the month should be in (strftime)</entry>
</row>
<row>
<entry>day_format</entry>
<entry>string</entry>
<entry>No</entry>
<entry>%02d</entry>
<entry>what format the day should be in (sprintf)</entry>
</row>
<row>
<entry>year_as_text</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>whether or not to display the year as text</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-05 14:40:26 +00:00
<para>
html_select_date is a custom function that creates date dropdowns
2001-02-06 22:17:51 +00:00
for you. It can display any or all of year, month, and day.
2001-01-05 14:40:26 +00:00
</para>
<example>
2001-01-26 17:47:59 +00:00
<title>html_select_date</title>
2001-01-05 14:40:26 +00:00
<programlisting>
{html_select_date}
2001-01-09 17:11:32 +00:00
OUTPUT:
<select name="Date_Month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12" selected>December</option>
</select>
<select name="Date_Day">
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected>13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="Date_Year">
<option value="2001" selected>2001</option>
</select>
</programlisting>
</example>
<example>
2001-01-26 17:47:59 +00:00
<title>html_select_date</title>
2001-01-09 17:11:32 +00:00
<programlisting>
2001-01-05 14:40:26 +00:00
2001-01-09 17:11:32 +00:00
{html_select_date prefix="StartDate" time=$time start_year=1995 end_year=2001 display_days=false}
2001-01-29 16:36:07 +00:00
OUTPUT:
2001-01-09 17:11:32 +00:00
<select name="StartDateMonth">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12" selected>December</option>
</select>
<select name="StartDateYear">
<option value="1999">1995</option>
<option value="1999">1996</option>
<option value="1999">1997</option>
<option value="1999">1998</option>
<option value="1999">1999</option>
<option value="2000" selected>2000</option>
<option value="2001">2001</option>
</select>
2001-01-05 14:40:26 +00:00
</programlisting>
</example>
</sect2>
<sect2>
<title>Creating your own Custom Functions</title>
<para>
Creating your own functions is a fairly straight forward process.
2001-01-09 17:11:32 +00:00
The best way is to look at the ones that come with Smarty as
examples. The function names begin with smarty_func_ and they are
located in the Smarty.addons.php file.
2001-01-05 14:40:26 +00:00
</para>
<itemizedlist>
<listitem><para>add your function to the Smarty.addons.php file.
2001-01-09 17:11:32 +00:00
It is recommended that you prepend your function name
2001-01-05 14:40:26 +00:00
with smarty_func_</para></listitem>
<listitem><para>map a template function name to your PHP function.
This is done at the top of the Smarty.class.php file
in the $custom_funcs array.</para></listitem>
<listitem><para>Thats it! you can now call that function
from within Smarty.</para></listitem>
</itemizedlist>
<para>
All attributes passed to custom functions are passed into the
2001-01-21 22:17:28 +00:00
first argument as an associative array. One way to get to those
2001-01-05 14:40:26 +00:00
values is to call extract(func_get_arg(0)); at the top of your
function. Anything that the function returns gets displayed
in place of the tag in the template.
</para>
2001-02-09 19:54:47 +00:00
<para>
You can also add custom functions programatically with the <link
linkend="api.register.function">register_function API</link>.
</para>
2001-01-04 20:33:27 +00:00
</sect2>
</sect1>
2001-01-12 20:39:27 +00:00
<sect1 id="variable.modifiers">
2001-01-05 14:40:26 +00:00
<title>Variable Modifiers</title>
<para>
2001-01-18 20:41:43 +00:00
Variable modifiers are a bit different than
<link linkend="custom.functions">custom functions</link>.
2001-02-06 22:17:51 +00:00
Variable modifiers alter variable contents before they are displayed to
the template. All modifiers will get the value of the variable as the
first argument, and must return a single value. Modifier parameters are
separated by colons. Any additional parameters passed to a modifier are
passed as-is positionally, much like calling a PHP function. You can
also use native PHP functions as modifiers, but only if they expect the
correct arguments. If they do not, you can always write a wrapper
function in Smarty to get what you want (date_format is a wrapper
function to strftime() for example.) You can chain as many modifiers
together on a variable as you like, separating each with a vertical
pipe "|".
</para>
<para>
NOTE: if you apply a modifier to an array instead of a single value
variable, the modifier will be applied to every value in that array. If
you really want the entire array passed to the modifier, you must
prepend it with an "@" sign like so: {$articleTitle|@count} (this will
print out the number of elements in the $articleTitle array.)
2001-01-05 14:40:26 +00:00
</para>
2001-02-06 22:17:51 +00:00
<sect2>
<title>capitalize</title>
<para>
This is used to capitalize the first letter of all words in a variable.
</para>
2001-01-05 14:40:26 +00:00
<example>
2001-02-06 22:17:51 +00:00
<title>capitalize</title>
2001-01-05 14:40:26 +00:00
<programlisting>
{* this displays a variable, unmodified *}
2001-02-06 22:17:51 +00:00
{$articleTitle|capitalize}
2001-01-05 14:40:26 +00:00
2001-01-09 17:11:32 +00:00
OUTPUT:
2001-02-06 22:17:51 +00:00
POLICE BEGIN CAMPAIGN TO RUNDOWN JAYWALKERS
2001-01-09 17:11:32 +00:00
2001-02-06 22:17:51 +00:00
</programlisting>
</example>
</sect2>
<sect2 id="date.format">
<title>date_format</title>
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry>%b %e, %Y</entry>
<entry>This is the format for the outputted date.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This formats a date and time into the given strftime() format. All
dates should be passed to Smarty as a timestamp so that the
template designer has full control of how this date is formatted.
</para>
<example>
<title>date_format</title>
<programlisting>
2001-01-09 17:11:32 +00:00
2001-02-06 22:17:51 +00:00
{$currentDate|date_format}
{$currentDate|date_format:"%A, %B %e, %Y"}
{$currentDate|date_format:"%H:%M:%S"}
2001-01-18 22:13:12 +00:00
OUTPUT:
2001-02-21 19:54:15 +00:00
Feb 6, 2001
2001-02-06 22:17:51 +00:00
Tuesday, February 6, 2001
14:33:00
2001-01-18 22:13:12 +00:00
2001-01-05 14:40:26 +00:00
</programlisting>
</example>
2001-01-09 17:11:32 +00:00
<example>
<title>date_format conversion specifiers</title>
<programlisting>
%a - abbreviated weekday name according to the current locale
%A - full weekday name according to the current locale
%b - abbreviated month name according to the current locale
%B - full month name according to the current locale
%c - preferred date and time representation for the current locale
%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
%d - day of the month as a decimal number (range 00 to 31)
%D - same as %m/%d/%y
2001-01-31 23:06:18 +00:00
%e - day of the month as a decimal number, a single digit is preceded by a
space (range 1 to 31)
%g - Week-based year within century [00,99]
%G - Week-based year, including the century [0000,9999]
2001-01-09 17:11:32 +00:00
%h - same as %b
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)
%I - hour as a decimal number using a 12-hour clock (range 01 to 12)
%j - day of the year as a decimal number (range 001 to 366)
2001-01-31 23:06:18 +00:00
%k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)
%l - hour as a decimal number using a 12-hour clock, single digits preceeded by
a space (range 1 to 12)
2001-01-09 17:11:32 +00:00
%m - month as a decimal number (range 01 to 12)
%M - minute as a decimal number
%n - newline character
%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second as a decimal number
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a decimal number [1,7], with 1 representing Monday
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1
is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
%w - day of the week as a decimal, Sunday being 0
2001-01-31 23:06:18 +00:00
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
2001-01-09 17:11:32 +00:00
%x - preferred date representation for the current locale without the time
%X - preferred time representation for the current locale without the date
%y - year as a decimal number without a century (range 00 to 99)
%Y - year as a decimal number including the century
%Z - time zone or name or abbreviation
%% - a literal `%' character
2001-01-31 23:06:18 +00:00
PROGRAMMERS NOTE: date_format is a wrapper to PHP's strftime() function. You
may have more or less conversion specifiers available depending on your
system's strftime() function where PHP was compiled. Check your system's manpage
for a full list of valid specifiers.
2001-01-09 17:11:32 +00:00
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-01-18 22:13:12 +00:00
<sect2>
<title>default</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>This is the default value to output if the
variable is empty.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-18 22:13:12 +00:00
<para>
This is used to set a default value for a variable. If the variable
is empty or unset, the given default value is printed instead.
2001-02-06 22:17:51 +00:00
Default takes one argument.
2001-01-18 22:13:12 +00:00
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>default</title>
<programlisting>
2001-02-21 19:54:15 +00:00
{* this will display "no title" (without the quotes) if $articleTitle is empty *}
2001-02-06 22:17:51 +00:00
{$articleTitle|default:"no title"}
OUTPUT:
no title
</programlisting>
</example>
2001-01-18 22:13:12 +00:00
</sect2>
2001-01-04 20:33:27 +00:00
<sect2>
<title>escape</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=posvals>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Possible Values</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry>html,url</entry>
<entry>html</entry>
<entry>This is the escape format to use.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-05 14:40:26 +00:00
<para>
This is used to html or url escape a variable. By default,
2001-02-06 22:17:51 +00:00
the variable is html escaped.
2001-01-05 14:40:26 +00:00
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>escape</title>
<programlisting>
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"}
{$articleTitle|escape:"url"}
OUTPUT:
Stiff Opposition Expected to Casketless Funeral Plan
Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan
Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan
Stiff+Opposition+Expected+to+Casketless+Funeral+Plan
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
2001-01-09 17:11:32 +00:00
<sect2>
<title>lower</title>
<para>
This is used to lowercase a variable.
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>lower</title>
<programlisting>
{$articleTitle}
{$articleTitle|lower}
OUTPUT:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.
</programlisting>
</example>
2001-01-09 17:11:32 +00:00
</sect2>
2001-01-04 20:33:27 +00:00
<sect2>
<title>replace</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the string of text to be replaced.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the string of text to replace with.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
A simple search and replace on a variable.
2001-01-05 14:40:26 +00:00
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>replace</title>
<programlisting>
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT:
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>spacify</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>one space</emphasis></entry>
<entry>This what gets inserted between each character of
the variable.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
spacify is a way to insert a space between every character of a variable.
2001-01-05 14:40:26 +00:00
You can optionally pass a different character (or string) to insert.
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>spacify</title>
<programlisting>
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT:
Something Went Wrong in Jet Crash, Experts Say.
S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^.
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>string_format</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is what format to use. (sprintf)</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-05 14:40:26 +00:00
<para>
This is a way to format strings, such as decimal numbers and such.
Use the syntax for sprintf for the formatting.
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>string_format</title>
<programlisting>
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT:
23.5787446
23.58
24
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>strip_tags</title>
2001-01-05 14:40:26 +00:00
<para>
This strips out markup tags, basically anything between < and >.
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>strip_tags</title>
<programlisting>
{$articleTitle}
{$articleTitle|strip_tags}
OUTPUT:
Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.
</programlisting>
</example>
2001-01-04 20:33:27 +00:00
</sect2>
<sect2>
<title>truncate</title>
2001-02-06 22:17:51 +00:00
<informaltable frame=all>
<tgroup cols=3>
<colspec colname=param>
<colspec colname=type>
<colspec colname=required>
<colspec colname=default>
<colspec colname=desc>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>80</entry>
<entry>This determines how many characters to truncate
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>...</entry>
<entry>This is the text to append if truncation occurs.</entry>
</row>
<row>
<entry>3</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>This determines whether or not to truncate at a
word boundary (false), or at the exact character (true).</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2001-01-05 14:40:26 +00:00
<para>
2001-01-09 17:11:32 +00:00
This truncates a variable to a character length, default is 80. As
2001-01-05 14:40:26 +00:00
an optional second parameter, you can specify a string of text
2001-01-18 20:41:43 +00:00
to display at the end if the variable was truncated. The
2001-01-09 17:11:32 +00:00
characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary. If
you want to cut off at the exact character length, pass the optional
third parameter of true.
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>truncate</title>
<programlisting>
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
</programlisting>
</example>
2001-01-09 17:11:32 +00:00
</sect2>
<sect2>
<title>upper</title>
<para>
This is used to uppercase a variable.
2001-01-05 14:40:26 +00:00
</para>
2001-02-06 22:17:51 +00:00
<example>
<title>upper</title>
<programlisting>
{$articleTitle}
{$articleTitle|upper}
OUTPUT:
If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
</programlisting>
</example>
2001-01-05 14:40:26 +00:00
</sect2>
<sect2>
<title>Creating your own Variable Modifiers</title>
<para>
Creating your own modifiers is a fairly straight forward process.
The best way is to look at the ones that come with Smarty as
2001-01-09 17:11:32 +00:00
examples. The function names begin with smarty_mod_ and they are
located in the Smarty.addons.php file.
2001-01-05 14:40:26 +00:00
</para>
<itemizedlist>
<listitem><para>add your modifier to the Smarty.addons.php file.
2001-01-09 17:11:32 +00:00
It is recommended that you prepend your function name
2001-01-05 14:40:26 +00:00
with smarty_mod_</para></listitem>
<listitem><para>map a template modifier name to your PHP function.
This is done at the top of the Smarty.class.php file
in the $custom_mods array.</para></listitem>
<listitem><para>Thats it! you can now use that modifier
from within Smarty.</para></listitem>
</itemizedlist>
2001-02-09 19:54:47 +00:00
<para>
You can also add modifiers programatically with the <link
linkend="api.register.modifier">register_modifier API</link>.
</para>
2001-01-04 20:33:27 +00:00
</sect2>
</sect1>
</chapter>
<chapter>
<title>Troubleshooting</title>
<para></para>
<sect1>
<title>Smarty/PHP errors</title>
2001-01-09 17:11:32 +00:00
<para>
2001-01-26 17:47:59 +00:00
Smarty can catch many errors such as missing tag attributes
or malformed variable names. If this happens, you will see an error
similar to the following:
</para>
<example>
<title>Smarty errors</title>
<programlisting>
Warning: Smarty: [in index.tpl line 4]: syntax error: unknown tag - '%blah'
in /path/to/smarty/Smarty.class.php on line 1041
Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name
in /path/to/smarty/Smarty.class.php on line 1041
</programlisting>
</example>
<para>
2001-02-09 19:54:47 +00:00
Smarty shows you the template name, the line number and the error.
After that, the error consists of the actual line number in the Smarty
class that the error occured.
2001-01-26 17:47:59 +00:00
</para>
<para>
2001-02-09 19:54:47 +00:00
There are certain errors that Smarty cannot catch, such as missing
close tags. These types of errors usually end up in PHP compile-time
parsing errors.
2001-01-26 17:47:59 +00:00
</para>
<example>
2001-02-09 19:54:47 +00:00
<title>PHP parsing errors</title>
2001-01-26 17:47:59 +00:00
<programlisting>
Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75
</programlisting>
</example>
<para>
2001-02-09 19:54:47 +00:00
When you encounter a PHP parsing error, the error line number will
correspond to the compiled PHP script, not the template itself. Usually
you can look at the template and spot the syntax error. Here are some
common things to look for: missing close tags for {if}{/if} or
{section}{/section}, or syntax of logic within an {if} tag. If you
can't find the error, you might have to open the compiled PHP file and
go to the line number to figure out where the corresponding error is in
the template.
2001-01-09 17:11:32 +00:00
</para>
2001-01-04 20:33:27 +00:00
</sect1>
</chapter>
2001-01-29 16:36:07 +00:00
<chapter id="tips">
<title>Tips & Tricks</title>
<para>
</para>
<sect1>
<title>Dates</title>
<para>
As a rule of thumb, always pass dates to Smarty as timestamps.
This allows template designers to use <link
linkend="date.format">date_format</link> for full control over date
formatting, and also makes it easy to compare dates if necessary.
</para>
<example>
<title>using date_format</title>
<programlisting>
{$startDate|date_format}
OUTPUT:
Jan 4, 2001
{$startDate|date_format:"%Y/%m/%d"}
OUTPUT:
2001/01/04
{if $date1 < $date2}
...
{/if}
</programlisting>
</example>
<para>
{html_select_date}, an included custom function with Smarty, also
expects a timestamp as the default value. When using {html_select_date}
in a template, The programmer will most likely want to convert the
output from the form back into timestamp format. Here is a function to
help you with that.
</para>
<example>
<title>converting form date elements back to a timestamp</title>
<programlisting>
// this assumes your form elements are named
// startDate_Day, startDate_Month, startDate_Year
$startDate = makeTimestamp($startDate_Year,$startDate_Month,$startDate_day);
function makeTimeStamp($year="",$month="",$day="")
{
if(empty($year))
$year = strftime("%Y");
if(empty($month))
$month = strftime("%m");
if(empty($day))
$day = strftime("%d");
return mktime(0,0,0,$month,$day,$year);
}
</programlisting>
</example>
</sect1>
</chapter>
2001-01-30 21:54:59 +00:00
<chapter id="resources">
<title>Resources</title>
<para>
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
subscribe-smarty@lists.ispi.net. An archive of the mailing list can be
viewed at http://marc.theaimsgroup.com/ under www/smarty.
</para>
</chapter>
2001-01-18 20:41:43 +00:00
<chapter id="bugs">
<title>BUGS</title>
2001-01-09 17:11:32 +00:00
<para>
2001-01-22 15:00:09 +00:00
Check the BUGS file that comes with the latest distribution of Smarty, or
check the website.
</para>
</chapter>
2001-01-22 15:40:36 +00:00
<chapter id="pear">
2001-01-22 15:00:09 +00:00
<title>PEAR</title>
<para>
Smarty uses the PEAR libraries for some of its error handling routines.
PEAR libraries come with the distribution of PHP. Be sure that the path to
2001-01-29 16:36:07 +00:00
these libraries is included in your php include_path. un*x users check
2001-01-22 15:00:09 +00:00
/usr/local/lib/php. Windows users check C:/php/pear.
2001-01-09 17:11:32 +00:00
</para>
2001-01-04 20:33:27 +00:00
</chapter>
2001-01-18 22:38:49 +00:00
<chapter id="credits">
<title>CREDITS</title>
<para>
2001-02-07 23:09:32 +00:00
Monte Ohrt <monte@ispi.net>: Concepted compiling templates into PHP
scripts, wrote initial "proof of concept" implementation, and maintains
documentation.
2001-01-18 22:38:49 +00:00
</para>
<para>
2001-02-07 23:09:32 +00:00
Andrei Zmievski <andrei@ispi.net>: Rewrote parser from scratch and
added other features too numerous to mention.
2001-01-18 22:38:49 +00:00
</para>
2001-01-22 23:18:04 +00:00
<para>
2001-02-07 23:09:32 +00:00
Anne Holz <anne@ispi.net>: Many of Smarty's formatting features were
a direct result of needs from her department.
2001-01-25 23:07:36 +00:00
</para>
<para>
Frank Kromann <fmk@php.net>: Idea of custom function ability.
</para>
<para>
2001-02-07 23:09:32 +00:00
A special thanks goes to the people that have contributed other templating
solutions to the PHP community which we learned a lot from.
2001-01-25 23:07:36 +00:00
</para>
<para>
2001-02-07 23:09:32 +00:00
A special thanks goes to the members of the php-template mailing list and
the smarty mailing list, whom shared and brought many ideas to the table.
2001-01-25 23:07:36 +00:00
</para>
<para>
Rasmus Lerdorf <rasmus@php.net>: For starting what eventually became
the coolest programming language ever.
2001-01-22 23:18:04 +00:00
</para>
2001-01-18 22:38:49 +00:00
</chapter>
2001-01-04 20:33:27 +00:00
</book>