mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-03 09:54:27 +02:00
added dislaimers
This commit is contained in:
530
README
530
README
@@ -1,7 +1,8 @@
|
||||
NAME:
|
||||
|
||||
Smarty - the PHP compiling template engine v0.9
|
||||
|
||||
Smarty - the PHP compiling template engine
|
||||
|
||||
VERSION: 1.0
|
||||
|
||||
AUTHORS:
|
||||
|
||||
@@ -28,8 +29,6 @@ DESCRIPTION:
|
||||
compiles the templates into native php scripts upon the first
|
||||
execution. After that, it just executes the compiled PHP scripts.
|
||||
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:
|
||||
|
||||
@@ -42,519 +41,42 @@ DESCRIPTION:
|
||||
the template language is extremely extensible.
|
||||
* configurable template delimiter tag syntax, so you can use
|
||||
{}, {{}}, <!--{}-->, or whatever you like.
|
||||
* template if/else/endif constructs are passed to the PHP parser,
|
||||
so the if syntax can be as simple or as complex as you like
|
||||
* 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.
|
||||
* unlimited nesting of sections,ifs, etc. allowed
|
||||
* 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:
|
||||
|
||||
Smarty requires PHP 4.0.4 or later (4.0.3 and earlier contain
|
||||
a bug in preg_grep() that won't allow the parser to function
|
||||
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.
|
||||
|
||||
Smarty requires PHP 4 or later. Smarty was developed and tested
|
||||
with 4.0.4pl1.
|
||||
|
||||
INSTALLATION:
|
||||
|
||||
* copy the Smarty.class.php and Smarty.addons.php scripts to a
|
||||
directory that is accessible by PHP. NOTE: Smarty will need to
|
||||
create a directory for the compiled templates. Be sure that the
|
||||
web server user (or which ever user the PHP parser is run as)
|
||||
can write to the directory. You will see appropriate error
|
||||
messages if the directory creation or php file creation fails.
|
||||
* copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php
|
||||
scripts to a directory that is in your PHP include path.
|
||||
|
||||
* in the same directory as your php application,
|
||||
create a "templates" directory and a "templates_c" directory.
|
||||
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
|
||||
included to get you started.
|
||||
|
||||
|
||||
EXAMPLE:
|
||||
|
||||
A simple example, built out of a few files:
|
||||
|
||||
index.php
|
||||
---------
|
||||
|
||||
<?
|
||||
|
||||
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}
|
||||
{/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} {/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%}
|
||||
{/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]}
|
||||
BUGS:
|
||||
|
||||
There are no known bugs with Smarty, although there are some
|
||||
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.
|
||||
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.
|
||||
|
||||
COPYRIGHT:
|
||||
Copyright(c) 2000,2001 ispi. All rights reserved.
|
||||
|
@@ -2,10 +2,37 @@
|
||||
|
||||
/*
|
||||
* Project: Smarty: the PHP compiled template engine
|
||||
* File: Smarty.functions.php
|
||||
* File: Smarty.addons.php
|
||||
* Author: Monte Ohrt <monte@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
|
||||
*
|
||||
*/
|
||||
|
||||
/*============================================*\
|
||||
|
@@ -8,6 +8,32 @@
|
||||
* Andrei Zmievski <andrei@ispi.net>
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
|
@@ -1,9 +1,12 @@
|
||||
{config_load file=test.conf section="my foo"}
|
||||
{include file=header.tpl title=foo}
|
||||
|
||||
<PRE>
|
||||
|
||||
Title: {#title#|capitalize}
|
||||
|
||||
{$SCRIPT_NAME}
|
||||
|
||||
the value of $SCRIPT_NAME is {$SCRIPT_NAME}
|
||||
|
||||
{* A simple variable test *}
|
||||
hello, my name is {$Name|upper}
|
||||
@@ -35,3 +38,5 @@ testing strip tags
|
||||
</tr>
|
||||
</table>
|
||||
{/strip}
|
||||
|
||||
</PRE>
|
||||
|
164
doc.sgm
164
doc.sgm
@@ -2,8 +2,18 @@
|
||||
<book>
|
||||
<bookinfo>
|
||||
<title>Smarty - the compiling PHP template engine</title>
|
||||
<author><firstname>Monte</firstname><surname>Ohrt</surname></author>
|
||||
<author><firstname>Andrei</firstname><surname>Zmievski</surname></author>
|
||||
<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>
|
||||
</bookinfo>
|
||||
<chapter>
|
||||
@@ -12,10 +22,11 @@
|
||||
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
|
||||
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
|
||||
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
|
||||
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>
|
||||
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
|
||||
have good template support because programmers don't want anything to
|
||||
do with HTML and don't want HTML designers mucking around with PHP
|
||||
code, and 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.
|
||||
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.
|
||||
</para>
|
||||
<para>
|
||||
Looking at many templating solutions available for PHP today, most of
|
||||
them provide a rudimentary way of substituting variables into templates
|
||||
and do a limited form of dynamic block functionality (a section of a
|
||||
template that is looped over and over with a set of indexed variables.)
|
||||
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.
|
||||
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.
|
||||
</para>
|
||||
<para>
|
||||
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,
|
||||
impenetrable. It was also noticably slow in large applications from all
|
||||
the parsing and regular expression work it had to do on each
|
||||
invocation. But the main problem from programmer's point of view was
|
||||
that you had to do a lot of work in the PHP script to setup and process
|
||||
templates and dynamic blocks especially.
|
||||
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?
|
||||
</para>
|
||||
<para>
|
||||
Then came the vision of what ultimately became Smarty. We know how fast
|
||||
@@ -103,7 +112,7 @@
|
||||
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,
|
||||
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>
|
||||
</itemizedlist>
|
||||
</sect1>
|
||||
@@ -133,33 +142,30 @@
|
||||
<sect1>
|
||||
<title>Requirements</title>
|
||||
<para>
|
||||
Smarty requires PHP 4 or later. 4.0.4 contains a bug that crashes PHP when
|
||||
the "@" modifier, such as {$var|@count}. 4.0.3
|
||||
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.
|
||||
Smarty requires PHP 4 or later. See the
|
||||
<link linkend="bugs">BUGS</link> section for caveats.
|
||||
</para>
|
||||
</sect1>
|
||||
<sect1>
|
||||
<title>Installing Smarty</title>
|
||||
<para>
|
||||
Installing Smarty is fairly straightforward, there is just one thing you
|
||||
must be aware of. Remember that Smarty creates compiled versions of the
|
||||
template code. 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
|
||||
Installing Smarty is fairly straightforward, there is just one thing
|
||||
you must be aware of. Remember that Smarty creates compiled versions of
|
||||
the template code. 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.
|
||||
</para>
|
||||
<para>
|
||||
Copy the Smarty.class.php and Smarty.addons.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.
|
||||
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.
|
||||
</para>
|
||||
<example>
|
||||
<title>Example of installing Smarty</title>
|
||||
@@ -181,9 +187,9 @@ chown nobody:nobody templates_c
|
||||
<chapter>
|
||||
<title>Setting up Smarty</title>
|
||||
<para>
|
||||
There are several variables that are at the top of the Smarty.class.php file. You
|
||||
can usually get away with leaving these at their default settings. This is a list
|
||||
of them and what each one does.
|
||||
There are several variables that are at the top of the Smarty.class.php
|
||||
file. You can usually get away with leaving these at their default
|
||||
settings. This is a list of them and what each one does.
|
||||
</para>
|
||||
<sect1>
|
||||
<title>Configuration variables</title>
|
||||
@@ -193,8 +199,8 @@ chown nobody:nobody templates_c
|
||||
<para>
|
||||
Upon each invocation of the PHP application, Smarty recursively
|
||||
traverses the template directory and its subdirectories and
|
||||
searches for all the files with the template extension that have
|
||||
changed (later time stamp) since the last time they were
|
||||
searches for all the files with the template extension 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. The compile
|
||||
check has very minimal impact on the application performance.
|
||||
@@ -226,8 +232,9 @@ chown nobody:nobody templates_c
|
||||
<sect2>
|
||||
<title>$tpl_file_ext</title>
|
||||
<para>
|
||||
This is the extention used for template files. By default this is ".tpl".
|
||||
All other files in the template directory are ignored.
|
||||
This is the extention used for template files. By default this
|
||||
is ".tpl". All other files in the template directory are
|
||||
ignored.
|
||||
</para>
|
||||
</sect2>
|
||||
<sect2>
|
||||
@@ -236,7 +243,7 @@ chown nobody:nobody templates_c
|
||||
Whether or not to allow PHP code in the templates. If set to
|
||||
false, PHP code is escaped and not interpreted. Embedding PHP
|
||||
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>
|
||||
</sect2>
|
||||
<sect2>
|
||||
@@ -268,7 +275,8 @@ chown nobody:nobody templates_c
|
||||
<sect2>
|
||||
<title>$custom_mods</title>
|
||||
<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.
|
||||
</para>
|
||||
</sect2>
|
||||
@@ -499,7 +507,7 @@ Your last login was on January 11th, 2001.
|
||||
Config file variables cannot be displayed until
|
||||
after they are loaded in from a config file. This procedure is
|
||||
explained later in this document under
|
||||
<link linkend="builtin.functions.config_load">config_load</link>.
|
||||
<link linkend="builtin.functions.configload">config_load</link>.
|
||||
</para>
|
||||
</sect3>
|
||||
<sect3>
|
||||
@@ -537,8 +545,8 @@ Your last login was on January 11th, 2001.
|
||||
</programlisting>
|
||||
</example>
|
||||
<para>
|
||||
Both built-in functions and custom functions work exactly the same
|
||||
way syntactically.
|
||||
Both built-in functions and custom functions have the same syntax in
|
||||
the templates.
|
||||
</para>
|
||||
</sect2>
|
||||
<sect2>
|
||||
@@ -560,9 +568,9 @@ Your last login was on January 11th, 2001.
|
||||
|
||||
{include file=#includeFile#}
|
||||
|
||||
<SELECT name=company>
|
||||
<SELECT name=company>
|
||||
{html_options values=$vals selected=$selected output=$output}
|
||||
</SELECT>
|
||||
</SELECT>
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
@@ -588,9 +596,9 @@ Your last login was on January 11th, 2001.
|
||||
{include file=#includeFile#}
|
||||
|
||||
{* display dropdown lists *}
|
||||
<SELECT name=company>
|
||||
<SELECT name=company>
|
||||
{html_options values=$vals selected=$selected output=$output}
|
||||
</SELECT>
|
||||
</SELECT>
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
@@ -605,8 +613,8 @@ Your last login was on January 11th, 2001.
|
||||
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 include the Config_File.class.php
|
||||
In your PHP include path. The Config_File class can be found at
|
||||
http://www.phpinsider.com. Smarty will implicitly include the file if you
|
||||
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.
|
||||
</para>
|
||||
<example>
|
||||
@@ -650,7 +658,7 @@ Intro = """This is a value that spans more
|
||||
</para>
|
||||
<para>
|
||||
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>
|
||||
</sect1>
|
||||
<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
|
||||
functions with the same names, nor can you modify built-in functions.
|
||||
</para>
|
||||
<sect2 id="builtin.functions.config_load">
|
||||
<sect2 id="builtin.functions.configload">
|
||||
<title>config_load</title>
|
||||
<para>
|
||||
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
|
||||
for a function named insert_getBanner() in your PHP application, passing
|
||||
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
|
||||
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
|
||||
are then displayed in the template in place of the insert tag.
|
||||
In this example, Smarty would call this function:
|
||||
@@ -837,7 +845,7 @@ Intro = """This is a value that spans more
|
||||
...
|
||||
{/if}
|
||||
|
||||
{* you can also imbed php function calls, where appropriate *}
|
||||
{* you can also imbed php function calls *}
|
||||
{if count($var) gt 0}
|
||||
...
|
||||
{/if}
|
||||
@@ -933,7 +941,7 @@ OUTPUT:
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
<sect2 id="builtin.functions.sections">
|
||||
<sect2 id="builtin.functions.section">
|
||||
<title>section,sectionelse</title>
|
||||
<para>
|
||||
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
|
||||
and underscores. Sections can be nested, and the nested
|
||||
section names must be unique from each other. The loop variable
|
||||
determines the number of times the section will loop. sectionelse
|
||||
will be used if there are no values in the loop variable.
|
||||
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 (/).
|
||||
sectionelse is executed when there are no values in the loop
|
||||
@@ -1197,8 +1204,8 @@ the section was shown.
|
||||
<sect2>
|
||||
<title>strip</title>
|
||||
<para>
|
||||
Strip is another nice feature of the template engine. Many times
|
||||
you run into the issue where white space and carriage returns
|
||||
Many times web designers
|
||||
run into the issue where white space and carriage returns
|
||||
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
|
||||
@@ -1432,7 +1439,8 @@ OUTPUT:
|
||||
<sect1 id="variable.modifiers">
|
||||
<title>Variable Modifiers</title>
|
||||
<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 are displayed to the template. The best way to explain
|
||||
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>
|
||||
This truncates a variable to a character length, default is 80. As
|
||||
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.
|
||||
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
|
||||
@@ -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
|
||||
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.
|
||||
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.
|
||||
When you encounter a PHP error when attempting to display 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>
|
||||
</sect1>
|
||||
</chapter>
|
||||
<chapter>
|
||||
<title>Syntax Highlighting in editors</title>
|
||||
<chapter id="bugs">
|
||||
<title>BUGS</title>
|
||||
<para>
|
||||
We have created some syntax highlighting for Nedit, a freely downloadable
|
||||
text editor for X windows. You can find the files and instructions on the
|
||||
Smarty web site.
|
||||
There are no known bugs with Smarty, although there are some
|
||||
bugs in PHP that can cause problems with Smarty. preg_grep() previous
|
||||
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>
|
||||
</chapter>
|
||||
<chapter>
|
||||
<title>FAQ</title>
|
||||
<para></para>
|
||||
</chapter>
|
||||
</book>
|
||||
|
@@ -8,6 +8,32 @@
|
||||
* Andrei Zmievski <andrei@ispi.net>
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
|
@@ -1,9 +1,12 @@
|
||||
{config_load file=test.conf section="my foo"}
|
||||
{include file=header.tpl title=foo}
|
||||
|
||||
<PRE>
|
||||
|
||||
Title: {#title#|capitalize}
|
||||
|
||||
{$SCRIPT_NAME}
|
||||
|
||||
the value of $SCRIPT_NAME is {$SCRIPT_NAME}
|
||||
|
||||
{* A simple variable test *}
|
||||
hello, my name is {$Name|upper}
|
||||
@@ -35,3 +38,5 @@ testing strip tags
|
||||
</tr>
|
||||
</table>
|
||||
{/strip}
|
||||
|
||||
</PRE>
|
||||
|
Reference in New Issue
Block a user