added dislaimers

This commit is contained in:
mohrt
2001-01-18 20:41:43 +00:00
parent b5461c2c2a
commit 52e80776c6
7 changed files with 205 additions and 584 deletions

530
README
View File

@@ -1,7 +1,8 @@
NAME: NAME:
Smarty - the PHP compiling template engine v0.9 Smarty - the PHP compiling template engine
VERSION: 1.0
AUTHORS: AUTHORS:
@@ -28,8 +29,6 @@ DESCRIPTION:
compiles the templates into native php scripts upon the first compiles the templates into native php scripts upon the first
execution. After that, it just executes the compiled PHP scripts. execution. After that, it just executes the compiled PHP scripts.
Therefore, there is no costly template file parsing for each request. Therefore, there is no costly template file parsing for each request.
Smarty also has built-in page caching to minimize the regeneration
of unchanged content.
Some of Smarty's features: Some of Smarty's features:
@@ -42,519 +41,42 @@ DESCRIPTION:
the template language is extremely extensible. the template language is extremely extensible.
* configurable template delimiter tag syntax, so you can use * configurable template delimiter tag syntax, so you can use
{}, {{}}, <!--{}-->, or whatever you like. {}, {{}}, <!--{}-->, or whatever you like.
* template if/else/endif constructs are passed to the PHP parser, * template if/elseif/else/endif constructs are passed to the PHP parser,
so the if syntax can be as simple or as complex as you like so the if syntax can be as simple or as complex as you like.
* unlimited nesting of sections,ifs, etc. allowed * unlimited nesting of sections,ifs, etc. allowed
* it is possible to imbed PHP code right in your template files, * it is possible to imbed PHP code right in your template files,
although doubtfully needed since the engine is so customizable. although not recommended and doubtfully needed since the engine
is so customizable.
REQUIREMENTS: REQUIREMENTS:
Smarty requires PHP 4.0.4 or later (4.0.3 and earlier contain Smarty requires PHP 4 or later. Smarty was developed and tested
a bug in preg_grep() that won't allow the parser to function with 4.0.4pl1.
properly.)
VARIABLE TYPES:
Smarty has three different syntaxes for variables. One for script
variables (prefixed with $), one for config file variables
which are surrounded by # symbols, and one for variables that
are internal to the template language, which are surrounded by
% symbols.
Variable examples:
{$Name} <-- prints the value of $Name
{#tableBgColor#} <-- prints variable from config file
{if %News.index% is even} <-- checks if the index is even
...
{/if}
CLASS METHODS:
assign($var,$val)
-----------------
Assign variables to be used in the template engine.
append($var,$val)
-----------------
Append values to assigned variables
display($tpl_file)
----------------------
Print the results of a parsed template file
fetch($tpl_file)
--------------------------
Return the results of a parsed template file
clear_assign($var)
------------------
Clear an assigned template variable.
clear_all_assign()
------------------
Clear all the assigned template variables.
get_template_vars()
------------------
returns an array containing assigned template variables
CLASS VARIABLES: (default value in parenthesis)
$compile_check Whether or not to check for a needed compile.
To optimize performance, set this to
false once development is complete and
the scripts are initially compiled.
(true)
$template_dir Name of directory containing templates
$compile_dir_ext Extention to give the name of the compile
directory. For example, if your templates
are stored in a directory named
"templates", then all compiled template
files will be kept in "templates_c" in the
same directory. (_c)
$tpl_file_ext The extention used on template files. (.tpl)
All other files in the template directory
without this extention are ignored.
$max_recursion_depth The maximum recursion depth for template
files includes. This is to help catch an
infinite loop. 0 == unlimited. (10)
$allow_php Whether or not to allow PHP code in your
templates. If set to false, PHP code is
escaped. (false)
$left_delimiter The left delimiter of the template syntax.
For some development tools, it may be handy
to change the delimiters to something like
"<!-- {" and "} -->" respectfully. ({)
$right_delimiter The right delimiter of the template syntax. (})
$config_dir The name of the directory containing config files
$custom_tags An array containing the names of custom functions
and what function each are mapped to.
$modifiers An array containing the names of variable modifiers
and what function each are mapped to.
$global_assign An array contiaining the names of variables
that are implicitly assigned to the templates.
INSTALLATION: INSTALLATION:
* copy the Smarty.class.php and Smarty.addons.php scripts to a * copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php
directory that is accessible by PHP. NOTE: Smarty will need to scripts to a directory that is in your PHP include path.
create a directory for the compiled templates. Be sure that the
web server user (or which ever user the PHP parser is run as) * in the same directory as your php application,
can write to the directory. You will see appropriate error create a "templates" directory and a "templates_c" directory.
messages if the directory creation or php file creation fails. Be sure the "templates_c" directory is writable by your
web server user (usually nobody), or chmod 777 the directory if
you are not sure.
* setup your php and template files. A good working example is * setup your php and template files. A good working example is
included to get you started. included to get you started.
BUGS:
EXAMPLE:
There are no known bugs with Smarty, although there are some
A simple example, built out of a few files: bugs in PHP that cause problems with Smarty. preg_grep() previous
to 4.0.4 has a bug which Smarty has a built-in workaround for.
index.php PHP 4.0.4 has a bug with user callbacks which would cause this
--------- syntax in Smarty to crash PHP: {$varname|@modname}
Use PHP 4.0.4pl1 to fix this, or avoid using the "@" with
<? modifiers.
require("Smarty.class.php");
$smarty = new Smarty;
// simulate some variable assignments. Normally this
// would come from a database or other data source.
$smarty->assign("Name","Gomer Pyle");
$smarty->assign("loopvar",array("one","two","three","four"));
$smarty->assign("loopvar2",array("one","two","three","<four>"));
// now simply display the template
$smarty->display("./templates/index.tpl");
?>
templates/header.tpl
--------------------
<HTML>
<BODY>
<TITLE>My Homepage</TITLE>
templates/footer.tpl
--------------------
</BODY>
</HTML>
templates/index.tpl
-------------------
{* include the header.tpl file here *}
{include header.tpl}
hello, my name is {$Name}.<br>
{if $Name eq "Joe"}
I am Joe.<br>
{else}
I am not Joe.<br>
{/if}
{* now lets test a section loop *}
<p>
testing a loop:<br>
{* $loopvar is used to determine the number
of times the section is looped *}
{section name="outside" loop=$loopvar}
{* show the current loop iteration *}
current loop iteration is {$outside/rownum}<br>
{* show the current index value of $loopvar
within the "outside" section *}
loop var is {$outside/loopvar}<br>
{* now we'll loop through a nested section *}
{section name="inside" loop=$loopvar}
{* show the current index value of $loopvar
within the "inside" section *}
inside loop: {$inside/loopvar}<br>
{/section}
<p>
{/section}
<p>
{* display $Name as HTML escaped *}
Hello, my name is {$Name|htmlesc}
{* include the footer.tpl file here *}
{include footer.tpl}
INCLUDE LOGIC:
Smarty supports including other template files.
* you must supply the relative path to the
included template file from the default
template directory path.
{include file="header.tpl"}
* you can pass local variables to an included
template. The included template inherits all current
template variables, in addition to the given
ones:
{include file="header.tpl" title="main menu" border="yes"}
IF/ELSE LOGIC:
Smarty supports if/else logic like so:
{if $Name eq "John"}
I am John!
{elseif $Name eq "Joe"}
I am Joe!
{else}
I don't know who I am.
{/if}
* "eq", "ne","neq", "gt", "lt", "lte", "le", "gte" "ge",
"==","!=",">","<","<=",">=" are all valid conditional
qualifiers.
SECTIONS:
Sections are portions of the template that are meant to be looped.
Example:
(Assuming $LastName, $MiddleName and $FirstName have been assigned):
{section name="employees" loop=$LastName}
This employee is {$employees/LastName},{$employees/FirstName}
{$employees/MiddleName}<br>
{/section}
The "name" attribute of a section is the name of the section.
The "loop" attribute is the name of an array that determines
the number of times the section will be looped. In this example
if $LastName has four elements, the section will loop four times.
* ALL sections must be given a name
* Nested sections names MUST have unique names from one another
* All variables meant to be looped within a section
MUST have the section name prepended to the name
like so: {$section_name/variable_name}
* It is OK to mention variables of parent sections
within nested child sections.
* nothing in the section will display if the
looping array is unset.
Sections can be nested, like so:
{section name="employees" loop=$LastName}
This employee is {$employees/LastName},
{$employees/FirstName} {$employees/MiddleName}<br>
{section name="employee_jobs" loop=$JobDescription}
Available jobs for {$employees/FirstName}:
{$employee_jobs/JobDescription}<br>
{/section}
{/section}
SPECIAL FUNCTIONALITY:
There are some special functions that determine the
current loop iteration of a section. These are surrounded
with "%" characters, and they are:
rownum: current row, first row treated as 1
index: current row, first row treated as 0
The following are possible "is" expression types
for these functions:
odd: true if value is odd
even: true if value is even
mod: true if value is divisible by X
Examples:
{section name=month loop=$var}
{if %month.rownum% eq 4}
{* in 4th row of section loop *}
{/if}
{if %month.rownum% is even}
{* current rownum is even *}
{/if}
{if %month.rownum% is odd}
{* current rownum is odd *}
{/if}
{if %month.rownum% is even by 3}
{* each even row, grouped by 3.
so rows 1,2,3 are true (even),
4,5,6 are odd (false), etc *}
{/if}
{if %month.rownum% is odd by 3}
{* each odd row, grouped by 3.
so rows 1,2,3 are true (odd),
4,5,6 are even (false), etc *}
{/if}
{if %month.rownum% is mod 4}
{* true if current row is divisible by 4 *}
{/if}
{/section}
THE {strip} TAG:
A special tag called {strip} can be used to strip out
extra space, tabs or newlines at the beginning and end
of each template row within {strip} and {/strip}.
For example:
<TR>
<TD>
<TT>
{section name=row loop=$weekday}
{$row/weekday}&nbsp;
{/section}
</TT>
</TD>
</TR>
This will return unwanted results because of
the extra tabs and newlines contained within the <TT></TT>
tags. Normally to remedy this, you must run all
the lines together in the template:
<TR>
<TD>
<TT>{section name=row loop=$weekday}{$row/weekday}&nbsp;{/section}</TT>
</TD>
</TR>
As you can see, this quickly makes the template unreadable.
An alternate solution is to use the {strip} tag:
<TR>
<TD>
{strip}
<TT>
{section name=row loop=$weekday}
{%row.weekday%}&nbsp;
{/section}
</TT>
{/strip}
</TD>
</TR>
What this will do is strip out tabs, spaces and newlines
at the beginning and end of each line before outputting
the results. This helps keep the template file readable
without affecting the output. Only text between {strip}
and {/strip} is affected.
{ldelim} AND {rdelim} TAGS:
These are used in the template to output the literal
left and right delimiters. Normally these are
"{" and "}" if you are using the default delimiters.
VARIABLE MODIFIERS:
Optionally, you can modify variables on-the-fly by passing
them through variable modifiers. Several variable modifiers
come with the default template engine. For example, if you
want a variable to be all uppercase when it is displayed,
you can do the following:
{$Name|upper}
This will display the value of $Name in uppercase.
Notice the vertical pipe "|" between the variable name
and the modifier. This is how the template parser
distinguishes that you want to pass the variable through
a modifier before being displayed. Anything to the right
of the variable name after a pipe "|" is a variable
modifier.
Here is an example of printing the variable $Name
as HTML escaped:
{$Name|escape}
You are also allowed to mix any variable modifiers
on a single variable:
{$Name|upper|escape}
Variable modifiers are read from left to right. In the
above example, the variable will first be uppercased,
then HTML escaped, then finally displayed.
Variable modifiers are passed the value of of the
variable (or the results of the previous modifier)
as the first argument. They can also be passed additional
arguments by specifying them in context, separated by
colons. For example, lets say you want to display only
the first 40 characters of a variable:
{$article|length:40}
This will print out the first 40 characters of the
variable $article.
Variable modifiers are also allowed within template logic
constructs, such as {if ...} tags. Example:
{if $Name|upper eq "JOE"}
His name is JOE.
{else}
His name is not JOE.
{/if}
You may also use PHP functions as variable
modifiers. If a modifier is not found in the registered
template modifiers, it will be assumed that it is a PHP
function, and will be passed as so. Be careful though,
all modifiers must take the input as the first value
and return a value, otherwise it won't do much good!
Example of using a PHP function:
<PRE>
{$article|wordwrap:72}
</PRE>
This will print the value of $article wordwrapped at 72 chars.
TIP: For PHP functions that do not expect the variable
arguments in the correct order, you can write a variable
modifier "wrapper" that passes them in the correct order.
For instance, strftime() expects the date format first,
followed by the date. Smarty comes with a wrapper that passes
them in the correct order:
{$date|date_format:"%Y-%m-%d"}
This will pass the value of $date, followed by the specified
date format to your variable modifier "date_format". This
can in turn pass the values to strftime() in the correct
order, then return the results.
HINT: always pass dates to Smarty in a unix
timestamp format. Smarty can then use this to format the
date in countless ways with date_format.
CUSTOM FUNCTIONS:
You may create your own custom template functions. There are two
functions that come bundled with Smarty, which are the following:
{html_options ...} creates HTML options for dropdowns
{html_select_date ...} creates HTML date dropdowns
CONFIG FILES:
Smarty allows variables to be pulled in from config files to
Be used in the templates. Section names are optional. NOTE:
config file sections are not related to template looping
sections. They are two different things.
{config_load file="filename"}
{config_load file="filename" section="sectionname"}
Config files have the following format:
var = "value"
[ sectionname ]
var = "value"
[ sectionname2 ]
var = "value"
example:
index.tpl
---------
{config_load file="index.conf"}
<IMG SRC="{#imagePath#}/icon.gif">
index.conf
----------
title = "My Homepage"
imagePath = "/images"
creating your own:
To create your own custom functions or modifiers, do the following:
* edit the Smarty.addons.php file, add your function. Be sure
the function name does not interfere with the namespace of other
functions.
* edit the Smarty.class.php file, add your custom function name to the
appropriate array, and map it to a Smarty function or modifier.
* make a mention of your custom function in a template, like so:
{function_name [arguments]}, or {$var|modifier_name[:arg1:arg2]}
COPYRIGHT: COPYRIGHT:
Copyright(c) 2000,2001 ispi. All rights reserved. Copyright(c) 2000,2001 ispi. All rights reserved.

View File

@@ -2,10 +2,37 @@
/* /*
* Project: Smarty: the PHP compiled template engine * Project: Smarty: the PHP compiled template engine
* File: Smarty.functions.php * File: Smarty.addons.php
* Author: Monte Ohrt <monte@ispi.net> * Author: Monte Ohrt <monte@ispi.net>
* Andrei Zmievski <andrei@ispi.net> * Andrei Zmievski <andrei@ispi.net>
* *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* You may contact the authors of Smarty by e-mail at:
* monte@ispi.net
* andrei@ispi.net
*
* Or, write to:
* Monte Ohrt
* CTO, ispi
* 237 S. 70th suite 220
* Lincoln, NE 68510
*
* The latest version of Smarty can be obtained from:
* http://www.phpinsider.com
*
*/ */
/*============================================*\ /*============================================*\

View File

@@ -8,6 +8,32 @@
* Andrei Zmievski <andrei@ispi.net> * Andrei Zmievski <andrei@ispi.net>
* parsing engine rewrite and a lot more * parsing engine rewrite and a lot more
* *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* You may contact the authors of Smarty by e-mail at:
* monte@ispi.net
* andrei@ispi.net
*
* Or, write to:
* Monte Ohrt
* CTO, ispi
* 237 S. 70th suite 220
* Lincoln, NE 68510
*
* The latest version of Smarty can be obtained from:
* http://www.phpinsider.com
* *
*/ */

View File

@@ -1,9 +1,12 @@
{config_load file=test.conf section="my foo"} {config_load file=test.conf section="my foo"}
{include file=header.tpl title=foo} {include file=header.tpl title=foo}
<PRE>
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME}
the value of $SCRIPT_NAME is {$SCRIPT_NAME}
{* A simple variable test *} {* A simple variable test *}
hello, my name is {$Name|upper} hello, my name is {$Name|upper}
@@ -35,3 +38,5 @@ testing strip tags
</tr> </tr>
</table> </table>
{/strip} {/strip}
</PRE>

164
doc.sgm
View File

@@ -2,8 +2,18 @@
<book> <book>
<bookinfo> <bookinfo>
<title>Smarty - the compiling PHP template engine</title> <title>Smarty - the compiling PHP template engine</title>
<author><firstname>Monte</firstname><surname>Ohrt</surname></author> <author>
<author><firstname>Andrei</firstname><surname>Zmievski</surname></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>
<copyright><year>2001</year><holder>ispi, Inc.</holder></copyright> <copyright><year>2001</year><holder>ispi, Inc.</holder></copyright>
</bookinfo> </bookinfo>
<chapter> <chapter>
@@ -12,10 +22,11 @@
It is undoubtedly one of the most asked questions on the PHP mailing 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 lists: how do I make my PHP scripts independent of the layout? While
PHP is billed as "HTML embedded scripting language", after writing a PHP is billed as "HTML embedded scripting language", after writing a
couple of projects that mixed PHP and HTML freely one comes up with couple of projects that mixed PHP and HTML freely one comes up with the
the idea that separation of form and content is a Good Thing [TM]. In idea that separation of form and content is a Good Thing [TM]. In
addition, in many companies the roles of layout designer and programmer addition, in many companies the roles of layout designer and programmer
are separate. Consequently, the search for a templating solution ensues. are separate. Consequently, the search for a templating solution
ensues.
</para> </para>
<para> <para>
In our company for example, the development of an application goes on In our company for example, the development of an application goes on
@@ -28,22 +39,20 @@
between programming/HTML a couple of times. Thus, it's important to between programming/HTML a couple of times. Thus, it's important to
have good template support because programmers don't want anything 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 do with HTML and don't want HTML designers mucking around with PHP
code, and designers need support for config files, dynamic blocks and code. Designers need support for config files, dynamic blocks and other
other stuff, but they don't want to have to deal with intricacies of stuff, but they don't want to have to deal with intricacies of the PHP
the PHP programming language. programming language.
</para> </para>
<para> <para>
Looking at many templating solutions available for PHP today, most of Looking at many templating solutions available for PHP today, most of
them provide a rudimentary way of substituting variables into templates them provide a rudimentary way of substituting variables into templates
and do a limited form of dynamic block functionality (a section of a and do a limited form of dynamic block functionality. But our needs
template that is looped over and over with a set of indexed variables.) required a bit more than that. We didn't want programmers to be dealing
But our needs required a bit more than that. We didn't want programmers with HTML layout at ALL, but this was almost inevitable. For instance,
to be dealing with HTML layout at ALL, but this was almost inevitable. if a designer wanted background colors to alternate on dynamic blocks,
For instance, if a designer wanted background colors to alternate on this had to be worked out with the programmer in advance. We also
dynamic blocks, this had to be worked out with the programmer in needed designers to be able to use their own configuration files, and
advance. We also needed designers to be able to use their own pull variables from them into the templates. The list goes on.
configuration files, and pull variables from them into the templates.
The list goes on.
</para> </para>
<para> <para>
We started out writing out a spec for a template engine about a year We started out writing out a spec for a template engine about a year
@@ -63,9 +72,9 @@
expressions and the code turned out to be rather, shall we say, expressions and the code turned out to be rather, shall we say,
impenetrable. It was also noticably slow in large applications from all impenetrable. It was also noticably slow in large applications from all
the parsing and regular expression work it had to do on each the parsing and regular expression work it had to do on each
invocation. But the main problem from programmer's point of view was invocation. The biggest problem from a programmer's point of view was
that you had to do a lot of work in the PHP script to setup and process all the necessary work in the PHP script to setup and process templates
templates and dynamic blocks especially. and dynamic blocks. How do we make this easier?
</para> </para>
<para> <para>
Then came the vision of what ultimately became Smarty. We know how fast Then came the vision of what ultimately became Smarty. We know how fast
@@ -103,7 +112,7 @@
as you like.</para></listitem> as you like.</para></listitem>
<listitem><para>Unlimited nesting of sections, ifs, etc. allowed.</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, <listitem><para>It is possible to embed PHP code right in your template files,
although this may not be needed (and definately not recommended) although this may not be needed (nor recommended)
since the engine is so customizable.</para></listitem> since the engine is so customizable.</para></listitem>
</itemizedlist> </itemizedlist>
</sect1> </sect1>
@@ -133,33 +142,30 @@
<sect1> <sect1>
<title>Requirements</title> <title>Requirements</title>
<para> <para>
Smarty requires PHP 4 or later. 4.0.4 contains a bug that crashes PHP when Smarty requires PHP 4 or later. See the
the "@" modifier, such as {$var|@count}. 4.0.3 <link linkend="bugs">BUGS</link> section for caveats.
and earlier contain a bug in preg_grep() that won't allow the parser to
function properly, so to preserve performance Smarty will automatically
use built-in preg_grep() when it can and use en emulated version of it
otherwise.
</para> </para>
</sect1> </sect1>
<sect1> <sect1>
<title>Installing Smarty</title> <title>Installing Smarty</title>
<para> <para>
Installing Smarty is fairly straightforward, there is just one thing you Installing Smarty is fairly straightforward, there is just one thing
must be aware of. Remember that Smarty creates compiled versions of the you must be aware of. Remember that Smarty creates compiled versions of
template code. This usually means allowing user "nobody" (or whomever the template code. This usually means allowing user "nobody" (or
the web server runs as) to have permission to write the files. Each whomever the web server runs as) to have permission to write the files.
installation of a Smarty application minimally needs a templates Each installation of a Smarty application minimally needs a templates
directory and a compiled templates directory. If you use configuration directory and a compiled templates directory. If you use configuration
files you will also need a directory for those. By default these are files you will also need a directory for those. By default these are
named "templates", and "templates_c" and "configs" respectively. named "templates", and "templates_c" and "configs" respectively.
</para> </para>
<para> <para>
Copy the Smarty.class.php and Smarty.addons.php scripts to a directory Copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php
that is in your PHP include_path. NOTE: PHP will try to create a scripts to a directory that is in your PHP include_path. NOTE: PHP will
directory alongside the executing script called "templates_c". Be sure try to create a directory alongside the executing script called
that directory permissions allow this to happen. You will see PHP error "templates_c". Be sure that directory permissions allow this to happen.
messages if this fails. You can also create the directory yourself You will see PHP error messages if this fails. You can also create the
before hand, and change the file ownership accordingly. See below. directory yourself before hand, and change the file ownership
accordingly. See below.
</para> </para>
<example> <example>
<title>Example of installing Smarty</title> <title>Example of installing Smarty</title>
@@ -181,9 +187,9 @@ chown nobody:nobody templates_c
<chapter> <chapter>
<title>Setting up Smarty</title> <title>Setting up Smarty</title>
<para> <para>
There are several variables that are at the top of the Smarty.class.php file. You There are several variables that are at the top of the Smarty.class.php
can usually get away with leaving these at their default settings. This is a list file. You can usually get away with leaving these at their default
of them and what each one does. settings. This is a list of them and what each one does.
</para> </para>
<sect1> <sect1>
<title>Configuration variables</title> <title>Configuration variables</title>
@@ -193,8 +199,8 @@ chown nobody:nobody templates_c
<para> <para>
Upon each invocation of the PHP application, Smarty recursively Upon each invocation of the PHP application, Smarty recursively
traverses the template directory and its subdirectories and traverses the template directory and its subdirectories and
searches for all the files with the template extension that have searches for all the files with the template extension that
changed (later time stamp) since the last time they were have changed (later time stamp) since the last time they were
compiled. For each one that has changed, it recompiles that compiled. For each one that has changed, it recompiles that
template. By default this variable is set to true. The compile template. By default this variable is set to true. The compile
check has very minimal impact on the application performance. check has very minimal impact on the application performance.
@@ -226,8 +232,9 @@ chown nobody:nobody templates_c
<sect2> <sect2>
<title>$tpl_file_ext</title> <title>$tpl_file_ext</title>
<para> <para>
This is the extention used for template files. By default this is ".tpl". This is the extention used for template files. By default this
All other files in the template directory are ignored. is ".tpl". All other files in the template directory are
ignored.
</para> </para>
</sect2> </sect2>
<sect2> <sect2>
@@ -236,7 +243,7 @@ chown nobody:nobody templates_c
Whether or not to allow PHP code in the templates. If set to Whether or not to allow PHP code in the templates. If set to
false, PHP code is escaped and not interpreted. Embedding PHP false, PHP code is escaped and not interpreted. Embedding PHP
code into templates is highly discouraged. Use custom functions code into templates is highly discouraged. Use custom functions
or modifiers instead. Default is "false". or <link linkend="variable.modifiers">modifiers</link> instead. Default is "false".
</para> </para>
</sect2> </sect2>
<sect2> <sect2>
@@ -268,7 +275,8 @@ chown nobody:nobody templates_c
<sect2> <sect2>
<title>$custom_mods</title> <title>$custom_mods</title>
<para> <para>
This is a mapping of the names of variable modifiers in the template to This is a mapping of the names of variable
<link linkend="variable.modifiers">modifiers</link> in the template to
the names of functions in PHP. These are usually kept in Smarty.addons.php. the names of functions in PHP. These are usually kept in Smarty.addons.php.
</para> </para>
</sect2> </sect2>
@@ -499,7 +507,7 @@ Your last login was on January 11th, 2001.
Config file variables cannot be displayed until Config file variables cannot be displayed until
after they are loaded in from a config file. This procedure is after they are loaded in from a config file. This procedure is
explained later in this document under explained later in this document under
<link linkend="builtin.functions.config_load">config_load</link>. <link linkend="builtin.functions.configload">config_load</link>.
</para> </para>
</sect3> </sect3>
<sect3> <sect3>
@@ -537,8 +545,8 @@ Your last login was on January 11th, 2001.
</programlisting> </programlisting>
</example> </example>
<para> <para>
Both built-in functions and custom functions work exactly the same Both built-in functions and custom functions have the same syntax in
way syntactically. the templates.
</para> </para>
</sect2> </sect2>
<sect2> <sect2>
@@ -560,9 +568,9 @@ Your last login was on January 11th, 2001.
{include file=#includeFile#} {include file=#includeFile#}
<SELECT name=company> &lt;SELECT name=company&gt;
{html_options values=$vals selected=$selected output=$output} {html_options values=$vals selected=$selected output=$output}
</SELECT> &lt;/SELECT&gt;
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -588,9 +596,9 @@ Your last login was on January 11th, 2001.
{include file=#includeFile#} {include file=#includeFile#}
{* display dropdown lists *} {* display dropdown lists *}
<SELECT name=company> &lt;SELECT name=company&gt;
{html_options values=$vals selected=$selected output=$output} {html_options values=$vals selected=$selected output=$output}
</SELECT> &lt;/SELECT&gt;
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -605,8 +613,8 @@ Your last login was on January 11th, 2001.
and change the colors. With a config file, the colors can and change the colors. With a config file, the colors can
be kept in one place, and only one file needs to be updated. be kept in one place, and only one file needs to be updated.
Note that to use config files, you must include the Config_File.class.php Note that to use config files, you must include the Config_File.class.php
In your PHP include path. The Config_File class can be found at In your PHP include path. Config_File.class.php comes bundled with
http://www.phpinsider.com. Smarty will implicitly include the file if you Smarty. Smarty will implicitly include the file if you
don't already include it in your application. don't already include it in your application.
</para> </para>
<example> <example>
@@ -650,7 +658,7 @@ Intro = """This is a value that spans more
</para> </para>
<para> <para>
Config files are loaded into templates with the built-in function Config files are loaded into templates with the built-in function
called <link linkend="builtin.functions.config_load">config_load</link>. called <link linkend="builtin.functions.configload">config_load</link>.
</para> </para>
</sect1> </sect1>
<sect1 id="builtin.functions"> <sect1 id="builtin.functions">
@@ -660,7 +668,7 @@ Intro = """This is a value that spans more
are integral to the template language. You cannot create custom are integral to the template language. You cannot create custom
functions with the same names, nor can you modify built-in functions. functions with the same names, nor can you modify built-in functions.
</para> </para>
<sect2 id="builtin.functions.config_load"> <sect2 id="builtin.functions.configload">
<title>config_load</title> <title>config_load</title>
<para> <para>
This function is used for loading in variables from a This function is used for loading in variables from a
@@ -788,9 +796,9 @@ Intro = """This is a value that spans more
parameters #banner_location_id# and #site_id#. Smarty will look parameters #banner_location_id# and #site_id#. Smarty will look
for a function named insert_getBanner() in your PHP application, passing for a function named insert_getBanner() in your PHP application, passing
the values of #banner_location_id# and #site_id# as the first argument the values of #banner_location_id# and #site_id# as the first argument
in an indexed array. (All insert function names in in an indexed array. All insert function names in
your application must be prepended with "insert_" to remedy possible your application must be prepended with "insert_" to remedy possible
function name-space conflicts.) Your insert_getBanner() function should function name-space conflicts. Your insert_getBanner() function should
do something with the passed values and return the results. These results do something with the passed values and return the results. These results
are then displayed in the template in place of the insert tag. are then displayed in the template in place of the insert tag.
In this example, Smarty would call this function: In this example, Smarty would call this function:
@@ -837,7 +845,7 @@ Intro = """This is a value that spans more
... ...
{/if} {/if}
{* you can also imbed php function calls, where appropriate *} {* you can also imbed php function calls *}
{if count($var) gt 0} {if count($var) gt 0}
... ...
{/if} {/if}
@@ -933,7 +941,7 @@ OUTPUT:
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
<sect2 id="builtin.functions.sections"> <sect2 id="builtin.functions.section">
<title>section,sectionelse</title> <title>section,sectionelse</title>
<para> <para>
Template sections are used for looping over arrays of data. Template sections are used for looping over arrays of data.
@@ -942,8 +950,7 @@ OUTPUT:
section can be anything you like, made up of letters, numbers section can be anything you like, made up of letters, numbers
and underscores. Sections can be nested, and the nested and underscores. Sections can be nested, and the nested
section names must be unique from each other. The loop variable section names must be unique from each other. The loop variable
determines the number of times the section will loop. sectionelse determines the number of times the section will loop.
will be used if there are no values in the loop variable.
When printing a variable within a section, the section name When printing a variable within a section, the section name
must be prepended to the variable name, separated by a slash (/). must be prepended to the variable name, separated by a slash (/).
sectionelse is executed when there are no values in the loop sectionelse is executed when there are no values in the loop
@@ -1197,8 +1204,8 @@ the section was shown.
<sect2> <sect2>
<title>strip</title> <title>strip</title>
<para> <para>
Strip is another nice feature of the template engine. Many times Many times web designers
you run into the issue where white space and carriage returns run into the issue where white space and carriage returns
affect the output of the rendered HTML (browser "features"), so you affect the output of the rendered HTML (browser "features"), so you
must run all your tags together in the template to get the must run all your tags together in the template to get the
desired results. This usually ends up in unreadable or desired results. This usually ends up in unreadable or
@@ -1432,7 +1439,8 @@ OUTPUT:
<sect1 id="variable.modifiers"> <sect1 id="variable.modifiers">
<title>Variable Modifiers</title> <title>Variable Modifiers</title>
<para> <para>
Variable modifiers are a bit different than custom functions. Variable modifiers are a bit different than
<link linkend="custom.functions">custom functions</link>.
They do just what they sound like, they modify variables before They do just what they sound like, they modify variables before
they are displayed to the template. The best way to explain they are displayed to the template. The best way to explain
these are by example. these are by example.
@@ -1668,7 +1676,7 @@ is the first week that has at least 4 days in the current year, and with Monday
<para> <para>
This truncates a variable to a character length, default is 80. As This truncates a variable to a character length, default is 80. As
an optional second parameter, you can specify a string of text an optional second parameter, you can specify a string of text
to display at the end if the variable was indeed truncated. The to display at the end if the variable was truncated. The
characters in the string are included with the original truncation length. characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary. If 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 you want to cut off at the exact character length, pass the optional
@@ -1711,6 +1719,8 @@ is the first week that has at least 4 days in the current year, and with Monday
As of now, Smarty is not a validating template parser. This means that As of now, Smarty is not a validating template parser. This means that
the parser will blindly convert the template to PHP scripts, irregardless the parser will blindly convert the template to PHP scripts, irregardless
of any syntax errors in the markup tags that may be present in the template. of any syntax errors in the markup tags that may be present in the template.
Smarty can catch certain template errors like missing attributes to
functions, but not syntax errors like missing close tags.
These types of errors can end up in PHP run-time errors. These types of errors can end up in PHP run-time errors.
When you encounter a PHP error when attempting to display the When you encounter a PHP error when attempting to display the
template in a browser, the error line number will correspond to the template in a browser, the error line number will correspond to the
@@ -1724,16 +1734,16 @@ is the first week that has at least 4 days in the current year, and with Monday
</para> </para>
</sect1> </sect1>
</chapter> </chapter>
<chapter> <chapter id="bugs">
<title>Syntax Highlighting in editors</title> <title>BUGS</title>
<para> <para>
We have created some syntax highlighting for Nedit, a freely downloadable There are no known bugs with Smarty, although there are some
text editor for X windows. You can find the files and instructions on the bugs in PHP that can cause problems with Smarty. preg_grep() previous
Smarty web site. to 4.0.4 has a bug which Smarty accomodates with a built-in workaround.
PHP 4.0.4 has a bug with user callbacks which would cause this
syntax in Smarty to crash PHP: {$varname|@modname}
Use PHP 4.0.4pl1 to fix this, or avoid using the "@" with
<link linkend="variable.modifiers">modifiers</link>.
</para> </para>
</chapter> </chapter>
<chapter>
<title>FAQ</title>
<para></para>
</chapter>
</book> </book>

View File

@@ -8,6 +8,32 @@
* Andrei Zmievski <andrei@ispi.net> * Andrei Zmievski <andrei@ispi.net>
* parsing engine rewrite and a lot more * parsing engine rewrite and a lot more
* *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* You may contact the authors of Smarty by e-mail at:
* monte@ispi.net
* andrei@ispi.net
*
* Or, write to:
* Monte Ohrt
* CTO, ispi
* 237 S. 70th suite 220
* Lincoln, NE 68510
*
* The latest version of Smarty can be obtained from:
* http://www.phpinsider.com
* *
*/ */

View File

@@ -1,9 +1,12 @@
{config_load file=test.conf section="my foo"} {config_load file=test.conf section="my foo"}
{include file=header.tpl title=foo} {include file=header.tpl title=foo}
<PRE>
Title: {#title#|capitalize} Title: {#title#|capitalize}
{$SCRIPT_NAME}
the value of $SCRIPT_NAME is {$SCRIPT_NAME}
{* A simple variable test *} {* A simple variable test *}
hello, my name is {$Name|upper} hello, my name is {$Name|upper}
@@ -35,3 +38,5 @@ testing strip tags
</tr> </tr>
</table> </table>
{/strip} {/strip}
</PRE>