mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 19:04:27 +02:00
update changes
This commit is contained in:
449
README
449
README
@@ -1,145 +1,158 @@
|
|||||||
NAME:
|
NAME:
|
||||||
|
|
||||||
Smarty - the PHP compiling template engine v0.9
|
Smarty - the PHP compiling template engine v0.9
|
||||||
|
|
||||||
|
|
||||||
AUTHORS:
|
AUTHORS:
|
||||||
|
|
||||||
Monte Ohrt <monte@ispi.net>
|
Monte Ohrt <monte@ispi.net>
|
||||||
Andrei Zmievski <andrei@ispi.net>
|
Andrei Zmievski <andrei@ispi.net>
|
||||||
|
|
||||||
SYNOPSIS:
|
SYNOPSIS:
|
||||||
|
|
||||||
require("smarty.class.php");
|
require("Smarty.class.php");
|
||||||
|
|
||||||
$smarty = new Smarty;
|
$smarty = new Smarty;
|
||||||
|
|
||||||
$smarty->assign("Title","My Homepage");
|
$smarty->assign("Title","My Homepage");
|
||||||
$smarty->assign("Names",array("John","Gary","Gregg","James"));
|
$smarty->assign("Names",array("John","Gary","Gregg","James"));
|
||||||
|
|
||||||
$smarty->spew("./templates/index.tpl");
|
$smarty->display("./templates/index.tpl");
|
||||||
|
|
||||||
DESCRIPTION:
|
DESCRIPTION:
|
||||||
|
|
||||||
What is Smarty?
|
What is Smarty?
|
||||||
|
|
||||||
Smarty is a template engine for PHP. One of the unique aspects about
|
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
|
Smarty that sets it apart from other templating solutions is that it
|
||||||
precompiles the templates into native php scripts upon the first
|
compiles the templates into native php scripts upon the first
|
||||||
execution, then executes the php scripts from that point forward.
|
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
|
Smarty also has built-in page caching to minimize the regeneration
|
||||||
of unchanged content.
|
of unchanged content.
|
||||||
|
|
||||||
Some of Smarty's features:
|
Some of Smarty's features:
|
||||||
|
|
||||||
* it is extremely fast
|
* it is extremely fast
|
||||||
* it is relatively simple since the PHP parser does the dirty work.
|
* it is relatively simple since the PHP parser does the dirty work.
|
||||||
* no template parsing overhead, only compiles once.
|
* no template parsing overhead, only compiles once.
|
||||||
* it is smart about automatically recompiling the template
|
* it is smart about recompiling only the template
|
||||||
files that have changed.
|
files that have changed.
|
||||||
* you can make custom functions and custom variable modifiers, so
|
* you can make custom functions and custom variable modifiers, so
|
||||||
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 your fancy.
|
{}, {{}}, <!--{}-->, or whatever you like.
|
||||||
* template if/else/endif constructs are passed to the PHP parser,
|
* 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
|
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 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.0.4 or later (4.0.3 and earlier contain
|
||||||
a bug in preg_grep() that won't allow the parser to function
|
a bug in preg_grep() that won't allow the parser to function
|
||||||
properly.)
|
properly.)
|
||||||
|
|
||||||
VARIABLE TYPES:
|
VARIABLE TYPES:
|
||||||
|
|
||||||
Smarty has two different syntaxes for variables. One for script
|
Smarty has three different syntaxes for variables. One for script
|
||||||
variables (prefixed with $), and one for internal variables
|
variables (prefixed with $), one for config file variables
|
||||||
generated by the template parser (prefixed and suffixed with %).
|
which are surrounded by # symbols, and one for variables that
|
||||||
|
are internal to the template language, which are surrounded by
|
||||||
|
% symbols.
|
||||||
|
|
||||||
Variable examples:
|
Variable examples:
|
||||||
{$Name} <-- prints the value of $Name
|
{$Name} <-- prints the value of $Name
|
||||||
{%News.rownum%} <-- prints the current iteration
|
{#tableBgColor#} <-- prints variable from config file
|
||||||
of the section named "News"
|
{if %News.index% is even} <-- checks if the index is even
|
||||||
{if %News.rownum% is even} <-- checks if the rownum is even
|
...
|
||||||
...
|
{/if}
|
||||||
{/if}
|
|
||||||
|
|
||||||
CLASS METHODS:
|
CLASS METHODS:
|
||||||
|
|
||||||
assign($var,$val)
|
assign($var,$val)
|
||||||
-----------------
|
-----------------
|
||||||
Assign variables to be used in the template engine.
|
Assign variables to be used in the template engine.
|
||||||
|
|
||||||
spew($tpl_file)
|
append($var,$val)
|
||||||
----------------------
|
-----------------
|
||||||
Print the results of a parsed template file
|
Append values to assigned variables
|
||||||
|
|
||||||
fetch($tpl_file)
|
|
||||||
--------------------------
|
|
||||||
Return the results of a parsed template file
|
|
||||||
|
|
||||||
clear_assign($var)
|
display($tpl_file)
|
||||||
------------------
|
----------------------
|
||||||
Clear an assigned template variable.
|
Print the results of a parsed template file
|
||||||
|
|
||||||
|
fetch($tpl_file)
|
||||||
|
--------------------------
|
||||||
|
Return the results of a parsed template file
|
||||||
|
|
||||||
clear_all_assign()
|
clear_assign($var)
|
||||||
------------------
|
------------------
|
||||||
Clear all the assigned template variables.
|
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)
|
CLASS VARIABLES: (default value in parenthesis)
|
||||||
|
|
||||||
$compile_check Whether or not to check for a needed compile.
|
$compile_check Whether or not to check for a needed compile.
|
||||||
To optimize performance, set this to
|
To optimize performance, set this to
|
||||||
false once development is complete and
|
false once development is complete and
|
||||||
the scripts are initially compiled.
|
the scripts are initially compiled.
|
||||||
(true)
|
(true)
|
||||||
|
|
||||||
$template_dir Name of directory containing templates
|
$template_dir Name of directory containing templates
|
||||||
|
|
||||||
$compile_dir_ext Extention to give the name of the compile
|
$compile_dir_ext Extention to give the name of the compile
|
||||||
directory. For example, if your templates
|
directory. For example, if your templates
|
||||||
are stored in a directory named
|
are stored in a directory named
|
||||||
"templates", then all compiled template
|
"templates", then all compiled template
|
||||||
files will be kept in "templates_c" in the
|
files will be kept in "templates_c" in the
|
||||||
same directory. (_c)
|
same directory. (_c)
|
||||||
|
|
||||||
$tpl_file_ext The extention used on template files. (.tpl)
|
$tpl_file_ext The extention used on template files. (.tpl)
|
||||||
All other files in the template directory
|
All other files in the template directory
|
||||||
without this extention are ignored.
|
without this extention are ignored.
|
||||||
|
|
||||||
$max_recursion_depth The maximum recursion depth for template
|
$max_recursion_depth The maximum recursion depth for template
|
||||||
files includes. This is to help catch an
|
files includes. This is to help catch an
|
||||||
infinite loop. 0 == unlimited. (10)
|
infinite loop. 0 == unlimited. (10)
|
||||||
|
|
||||||
$allow_php Whether or not to allow PHP code in your
|
$allow_php Whether or not to allow PHP code in your
|
||||||
templates. If set to false, PHP code is
|
templates. If set to false, PHP code is
|
||||||
escaped. (false)
|
escaped. (false)
|
||||||
|
|
||||||
|
|
||||||
$left_delimiter The left delimiter of the template syntax.
|
$left_delimiter The left delimiter of the template syntax.
|
||||||
For some development tools, it may be handy
|
For some development tools, it may be handy
|
||||||
to change the delimiters to something like
|
to change the delimiters to something like
|
||||||
"<!-- {" and "} -->" respectfully. ({)
|
"<!-- {" and "} -->" respectfully. ({)
|
||||||
|
|
||||||
$right_delimiter The right delimiter of the template syntax. (})
|
$right_delimiter The right delimiter of the template syntax. (})
|
||||||
|
|
||||||
$registered_functions Names of the custom template functions
|
$config_dir The name of the directory containing config files
|
||||||
that Smarty will recognize.
|
|
||||||
To add your own, add them to this array
|
$custom_tags An array containing the names of custom functions
|
||||||
and add the actual functions to the
|
and what function each are mapped to.
|
||||||
smarty.functions.php file.
|
|
||||||
(array( "htmlesc","urlesc","default","config" );)
|
$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.functions.php scripts to a
|
* copy the Smarty.class.php and Smarty.addons.php scripts to a
|
||||||
directory that is accessible by PHP. NOTE: Smarty will need to
|
directory that is accessible by PHP. NOTE: Smarty will need to
|
||||||
create a directory for the compiled templates. Be sure that the
|
create a directory for the compiled templates. Be sure that the
|
||||||
web server user (or which ever user the PHP parser is run as)
|
web server user (or which ever user the PHP parser is run as)
|
||||||
@@ -159,7 +172,7 @@ index.php
|
|||||||
|
|
||||||
<?
|
<?
|
||||||
|
|
||||||
require("smarty.class.php");
|
require("Smarty.class.php");
|
||||||
|
|
||||||
$smarty = new Smarty;
|
$smarty = new Smarty;
|
||||||
|
|
||||||
@@ -171,7 +184,7 @@ $smarty->assign("loopvar",array("one","two","three","four"));
|
|||||||
$smarty->assign("loopvar2",array("one","two","three","<four>"));
|
$smarty->assign("loopvar2",array("one","two","three","<four>"));
|
||||||
|
|
||||||
// now simply display the template
|
// now simply display the template
|
||||||
$smarty->spew("./templates/index.tpl");
|
$smarty->display("./templates/index.tpl");
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
||||||
@@ -197,9 +210,9 @@ templates/index.tpl
|
|||||||
{include header.tpl}
|
{include header.tpl}
|
||||||
hello, my name is {$Name}.<br>
|
hello, my name is {$Name}.<br>
|
||||||
{if $Name eq "Joe"}
|
{if $Name eq "Joe"}
|
||||||
I am Joe.<br>
|
I am Joe.<br>
|
||||||
{else}
|
{else}
|
||||||
I am not Joe.<br>
|
I am not Joe.<br>
|
||||||
{/if}
|
{/if}
|
||||||
{* now lets test a section loop *}
|
{* now lets test a section loop *}
|
||||||
<p>
|
<p>
|
||||||
@@ -207,18 +220,18 @@ testing a loop:<br>
|
|||||||
{* $loopvar is used to determine the number
|
{* $loopvar is used to determine the number
|
||||||
of times the section is looped *}
|
of times the section is looped *}
|
||||||
{section name="outside" loop=$loopvar}
|
{section name="outside" loop=$loopvar}
|
||||||
{* show the current loop iteration *}
|
{* show the current loop iteration *}
|
||||||
current loop iteration is {%outside.rownum%}<br>
|
current loop iteration is {$outside/rownum}<br>
|
||||||
{* show the current index value of $loopvar
|
{* show the current index value of $loopvar
|
||||||
within the "outside" section *}
|
within the "outside" section *}
|
||||||
loop var is {$outside/loopvar}<br>
|
loop var is {$outside/loopvar}<br>
|
||||||
{* now we'll loop through a nested section *}
|
{* now we'll loop through a nested section *}
|
||||||
{section name="inside" loop=$loopvar}
|
{section name="inside" loop=$loopvar}
|
||||||
{* show the current index value of $loopvar
|
{* show the current index value of $loopvar
|
||||||
within the "inside" section *}
|
within the "inside" section *}
|
||||||
inside loop: {$inside/loopvar}<br>
|
inside loop: {$inside/loopvar}<br>
|
||||||
{/section}
|
{/section}
|
||||||
<p>
|
<p>
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -233,23 +246,30 @@ INCLUDE LOGIC:
|
|||||||
Smarty supports including other template files.
|
Smarty supports including other template files.
|
||||||
|
|
||||||
* you must supply the relative path to the
|
* you must supply the relative path to the
|
||||||
included template file from the template
|
included template file from the default
|
||||||
in use. Example: in index.tpl, the file
|
template directory path.
|
||||||
header.tpl is included from the same directory:
|
|
||||||
|
|
||||||
{include header.tpl}
|
{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:
|
IF/ELSE LOGIC:
|
||||||
|
|
||||||
Smarty supports if/else logic like so:
|
Smarty supports if/else logic like so:
|
||||||
|
|
||||||
{if $Name eq "John"}
|
{if $Name eq "John"}
|
||||||
I am John!
|
I am John!
|
||||||
|
{elseif $Name eq "Joe"}
|
||||||
|
I am Joe!
|
||||||
{else}
|
{else}
|
||||||
I am not John.
|
I don't know who I am.
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
A few important things to know:
|
|
||||||
* "eq", "ne","neq", "gt", "lt", "lte", "le", "gte" "ge",
|
* "eq", "ne","neq", "gt", "lt", "lte", "le", "gte" "ge",
|
||||||
"==","!=",">","<","<=",">=" are all valid conditional
|
"==","!=",">","<","<=",">=" are all valid conditional
|
||||||
qualifiers.
|
qualifiers.
|
||||||
@@ -263,8 +283,8 @@ Example:
|
|||||||
(Assuming $LastName, $MiddleName and $FirstName have been assigned):
|
(Assuming $LastName, $MiddleName and $FirstName have been assigned):
|
||||||
|
|
||||||
{section name="employees" loop=$LastName}
|
{section name="employees" loop=$LastName}
|
||||||
This employee is {$employees/LastName},{$employees/FirstName}
|
This employee is {$employees/LastName},{$employees/FirstName}
|
||||||
{$employees/MiddleName}<br>
|
{$employees/MiddleName}<br>
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
The "name" attribute of a section is the name of the section.
|
The "name" attribute of a section is the name of the section.
|
||||||
@@ -272,9 +292,8 @@ The "loop" attribute is the name of an array that determines
|
|||||||
the number of times the section will be looped. In this example
|
the number of times the section will be looped. In this example
|
||||||
if $LastName has four elements, the section will loop four times.
|
if $LastName has four elements, the section will loop four times.
|
||||||
|
|
||||||
A few important things to know:
|
* ALL sections must be given a name
|
||||||
* ALL sections must be given a name.
|
* Nested sections names MUST have unique names from one another
|
||||||
* ALL section names MUST be unique from one another.
|
|
||||||
* All variables meant to be looped within a section
|
* All variables meant to be looped within a section
|
||||||
MUST have the section name prepended to the name
|
MUST have the section name prepended to the name
|
||||||
like so: {$section_name/variable_name}
|
like so: {$section_name/variable_name}
|
||||||
@@ -286,12 +305,12 @@ A few important things to know:
|
|||||||
Sections can be nested, like so:
|
Sections can be nested, like so:
|
||||||
|
|
||||||
{section name="employees" loop=$LastName}
|
{section name="employees" loop=$LastName}
|
||||||
This employee is {$employees/LastName},
|
This employee is {$employees/LastName},
|
||||||
{$employees/FirstName} {$employees/MiddleName}<br>
|
{$employees/FirstName} {$employees/MiddleName}<br>
|
||||||
{section name="employee_jobs" loop=$JobDescription}
|
{section name="employee_jobs" loop=$JobDescription}
|
||||||
Available jobs for {$employees/FirstName}:
|
Available jobs for {$employees/FirstName}:
|
||||||
{$employee_jobs/JobDescription}<br>
|
{$employee_jobs/JobDescription}<br>
|
||||||
{/section}
|
{/section}
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
SPECIAL FUNCTIONALITY:
|
SPECIAL FUNCTIONALITY:
|
||||||
@@ -300,41 +319,41 @@ There are some special functions that determine the
|
|||||||
current loop iteration of a section. These are surrounded
|
current loop iteration of a section. These are surrounded
|
||||||
with "%" characters, and they are:
|
with "%" characters, and they are:
|
||||||
|
|
||||||
rownum: current row, first row treated as 1
|
rownum: current row, first row treated as 1
|
||||||
index: current row, first row treated as 0
|
index: current row, first row treated as 0
|
||||||
|
|
||||||
The following are possible "is" expression types
|
The following are possible "is" expression types
|
||||||
for these functions:
|
for these functions:
|
||||||
|
|
||||||
odd: true if value is odd
|
odd: true if value is odd
|
||||||
even: true if value is even
|
even: true if value is even
|
||||||
mod: true if value is divisible by X
|
mod: true if value is divisible by X
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
{section name=month loop=$var}
|
{section name=month loop=$var}
|
||||||
{if %month.rownum% eq 4}
|
{if %month.rownum% eq 4}
|
||||||
{* in 4th row of section loop *}
|
{* in 4th row of section loop *}
|
||||||
{/if}
|
{/if}
|
||||||
{if %month.rownum% is even}
|
{if %month.rownum% is even}
|
||||||
{* current rownum is even *}
|
{* current rownum is even *}
|
||||||
{/if}
|
{/if}
|
||||||
{if %month.rownum% is odd}
|
{if %month.rownum% is odd}
|
||||||
{* current rownum is odd *}
|
{* current rownum is odd *}
|
||||||
{/if}
|
{/if}
|
||||||
{if %month.rownum% is even by 3}
|
{if %month.rownum% is even by 3}
|
||||||
{* each even row, grouped by 3.
|
{* each even row, grouped by 3.
|
||||||
so rows 1,2,3 are true (even),
|
so rows 1,2,3 are true (even),
|
||||||
4,5,6 are odd (false), etc *}
|
4,5,6 are odd (false), etc *}
|
||||||
{/if}
|
{/if}
|
||||||
{if %month.rownum% is odd by 3}
|
{if %month.rownum% is odd by 3}
|
||||||
{* each odd row, grouped by 3.
|
{* each odd row, grouped by 3.
|
||||||
so rows 1,2,3 are true (odd),
|
so rows 1,2,3 are true (odd),
|
||||||
4,5,6 are even (false), etc *}
|
4,5,6 are even (false), etc *}
|
||||||
{/if}
|
{/if}
|
||||||
{if %month.rownum% is mod 4}
|
{if %month.rownum% is mod 4}
|
||||||
{* true if current row is divisible by 4 *}
|
{* true if current row is divisible by 4 *}
|
||||||
{/if}
|
{/if}
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
THE {strip} TAG:
|
THE {strip} TAG:
|
||||||
@@ -346,38 +365,38 @@ For example:
|
|||||||
|
|
||||||
<TR>
|
<TR>
|
||||||
<TD>
|
<TD>
|
||||||
<TT>
|
<TT>
|
||||||
{section name=row loop=$weekday}
|
{section name=row loop=$weekday}
|
||||||
{%row.weekday%}
|
{$row/weekday}
|
||||||
{/section}
|
{/section}
|
||||||
</TT>
|
</TT>
|
||||||
</TD>
|
</TD>
|
||||||
</TR>
|
</TR>
|
||||||
|
|
||||||
This will return unwanted results because of
|
This will return unwanted results because of
|
||||||
the extra tabs and newlines contained within the <TT></TT>
|
the extra tabs and newlines contained within the <TT></TT>
|
||||||
tags. Normally to remedy this, you must run all
|
tags. Normally to remedy this, you must run all
|
||||||
the lines together in the template like so:
|
the lines together in the template:
|
||||||
|
|
||||||
<TR>
|
<TR>
|
||||||
<TD>
|
<TD>
|
||||||
<TT>{section name=row loop=$weekday}{%row.weekday%} {/section}</TT>
|
<TT>{section name=row loop=$weekday}{$row/weekday} {/section}</TT>
|
||||||
</TD>
|
</TD>
|
||||||
</TR>
|
</TR>
|
||||||
|
|
||||||
|
|
||||||
As you can see, this quickly makes the template unreadable.
|
As you can see, this quickly makes the template unreadable.
|
||||||
An alternate solution is to use the {strip} tag like so:
|
An alternate solution is to use the {strip} tag:
|
||||||
|
|
||||||
<TR>
|
<TR>
|
||||||
<TD>
|
<TD>
|
||||||
{strip}
|
{strip}
|
||||||
<TT>
|
<TT>
|
||||||
{section name=row loop=$weekday}
|
{section name=row loop=$weekday}
|
||||||
{%row.weekday%}
|
{%row.weekday%}
|
||||||
{/section}
|
{/section}
|
||||||
</TT>
|
</TT>
|
||||||
{/strip}
|
{/strip}
|
||||||
</TD>
|
</TD>
|
||||||
</TR>
|
</TR>
|
||||||
|
|
||||||
@@ -388,7 +407,7 @@ the results. This helps keep the template file readable
|
|||||||
without affecting the output. Only text between {strip}
|
without affecting the output. Only text between {strip}
|
||||||
and {/strip} is affected.
|
and {/strip} is affected.
|
||||||
|
|
||||||
{%ldelim%} AND {%rdelim%} TAGS:
|
{ldelim} AND {rdelim} TAGS:
|
||||||
|
|
||||||
These are used in the template to output the literal
|
These are used in the template to output the literal
|
||||||
left and right delimiters. Normally these are
|
left and right delimiters. Normally these are
|
||||||
@@ -415,12 +434,12 @@ modifier.
|
|||||||
Here is an example of printing the variable $Name
|
Here is an example of printing the variable $Name
|
||||||
as HTML escaped:
|
as HTML escaped:
|
||||||
|
|
||||||
{$Name|htmlesc}
|
{$Name|escape}
|
||||||
|
|
||||||
You are also allowed to mix any variable modifiers
|
You are also allowed to mix any variable modifiers
|
||||||
on a single variable:
|
on a single variable:
|
||||||
|
|
||||||
{$Name|upper|htmlesc}
|
{$Name|upper|escape}
|
||||||
|
|
||||||
Variable modifiers are read from left to right. In the
|
Variable modifiers are read from left to right. In the
|
||||||
above example, the variable will first be uppercased,
|
above example, the variable will first be uppercased,
|
||||||
@@ -442,9 +461,9 @@ Variable modifiers are also allowed within template logic
|
|||||||
constructs, such as {if ...} tags. Example:
|
constructs, such as {if ...} tags. Example:
|
||||||
|
|
||||||
{if $Name|upper eq "JOE"}
|
{if $Name|upper eq "JOE"}
|
||||||
His name is JOE.
|
His name is JOE.
|
||||||
{else}
|
{else}
|
||||||
His name is not JOE.
|
His name is not JOE.
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
You may also use PHP functions as variable
|
You may also use PHP functions as variable
|
||||||
@@ -452,7 +471,7 @@ modifiers. If a modifier is not found in the registered
|
|||||||
template modifiers, it will be assumed that it is a PHP
|
template modifiers, it will be assumed that it is a PHP
|
||||||
function, and will be passed as so. Be careful though,
|
function, and will be passed as so. Be careful though,
|
||||||
all modifiers must take the input as the first value
|
all modifiers must take the input as the first value
|
||||||
and return a modified value, otherwise it won't do much good!
|
and return a value, otherwise it won't do much good!
|
||||||
|
|
||||||
Example of using a PHP function:
|
Example of using a PHP function:
|
||||||
|
|
||||||
@@ -466,31 +485,47 @@ TIP: For PHP functions that do not expect the variable
|
|||||||
arguments in the correct order, you can write a variable
|
arguments in the correct order, you can write a variable
|
||||||
modifier "wrapper" that passes them in the correct order.
|
modifier "wrapper" that passes them in the correct order.
|
||||||
For instance, strftime() expects the date format first,
|
For instance, strftime() expects the date format first,
|
||||||
followed by the date. You can write a wrapper that passes
|
followed by the date. Smarty comes with a wrapper that passes
|
||||||
them in the correct order:
|
them in the correct order:
|
||||||
|
|
||||||
{$date|dateFormat:"%Y-%m-%d"}
|
{$date|date_format:"%Y-%m-%d"}
|
||||||
|
|
||||||
This will pass the value of $date, followed by the specified
|
This will pass the value of $date, followed by the specified
|
||||||
date format to your variable modifier "dateFormat". This
|
date format to your variable modifier "date_format". This
|
||||||
can in turn pass the values to strftime() in the correct
|
can in turn pass the values to strftime() in the correct
|
||||||
order, then return the results.
|
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:
|
CUSTOM FUNCTIONS:
|
||||||
|
|
||||||
You may create your own custom template functions. There are four
|
You may create your own custom template functions. There are two
|
||||||
functions that come bundled with Smarty, which are the following:
|
functions that come bundled with Smarty, which are the following:
|
||||||
|
|
||||||
{htmlesc $var} prints a variable html escaped.
|
{html_options ...} creates HTML options for dropdowns
|
||||||
{urlesc $var} prints a variable url escaped.
|
{html_select_date ...} creates HTML date dropdowns
|
||||||
{default $var,"default value"} prints the default value if
|
|
||||||
$var is empty.
|
|
||||||
{config "file.conf","var"} prints the value of a variable
|
|
||||||
from a config file.
|
|
||||||
|
|
||||||
For this "config" function, config files must be in the following syntax:
|
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"
|
var = "value"
|
||||||
|
|
||||||
example:
|
example:
|
||||||
@@ -498,28 +533,30 @@ example:
|
|||||||
index.tpl
|
index.tpl
|
||||||
---------
|
---------
|
||||||
|
|
||||||
{config "config/index.conf","title"}
|
{config_load file="index.conf"}
|
||||||
|
|
||||||
|
<IMG SRC="{#imagePath#}/icon.gif">
|
||||||
|
|
||||||
index.conf
|
index.conf
|
||||||
----------
|
----------
|
||||||
|
|
||||||
title = "My Homepage"
|
title = "My Homepage"
|
||||||
Name = "Gomer"
|
imagePath = "/images"
|
||||||
|
|
||||||
|
|
||||||
creating your own:
|
creating your own:
|
||||||
|
|
||||||
To create your own custom functions, do the following:
|
To create your own custom functions or modifiers, do the following:
|
||||||
|
|
||||||
* edit the smarty.functions.php file, add your custom function. Be sure
|
* edit the Smarty.addons.php file, add your function. Be sure
|
||||||
the function name is prepended with "smarty_".
|
the function name does not interfere with the namespace of other
|
||||||
* edit the smarty.class.php file, add your custom function name to the
|
functions.
|
||||||
array (leave off the smarty_ prefix.)
|
* 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:
|
* make a mention of your custom function in a template, like so:
|
||||||
{function_name [arguments]}
|
{function_name [arguments]}, or {$var|modifier_name[:arg1:arg2]}
|
||||||
* The arguments are passed "as is" to the custom function.
|
|
||||||
|
|
||||||
COPYRIGHT:
|
COPYRIGHT:
|
||||||
Copyright(c) 2000 ispi. All rights reserved.
|
Copyright(c) 2000,2001 ispi. All rights reserved.
|
||||||
This software is released under the GNU General Public License.
|
This software is released under the GNU General Public License.
|
||||||
Please read the disclaimer at the top of the Smarty.class.php file.
|
Please read the disclaimer at the top of the Smarty.class.php file.
|
||||||
|
@@ -41,11 +41,11 @@ class Smarty
|
|||||||
|
|
||||||
var $config_dir = "./configs"; // directory where config files are located
|
var $config_dir = "./configs"; // directory where config files are located
|
||||||
|
|
||||||
var $custom_tags = array( 'html_options' => 'smarty_func_html_options',
|
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
|
||||||
'html_select_date' => 'smarty_func_html_select_date'
|
'html_select_date' => 'smarty_func_html_select_date'
|
||||||
);
|
);
|
||||||
|
|
||||||
var $modifiers = array( 'lower' => 'strtolower',
|
var $custom_mods = array( 'lower' => 'strtolower',
|
||||||
'upper' => 'strtoupper',
|
'upper' => 'strtoupper',
|
||||||
'capitalize' => 'ucwords',
|
'capitalize' => 'ucwords',
|
||||||
'escape' => 'smarty_mod_escape',
|
'escape' => 'smarty_mod_escape',
|
||||||
@@ -59,6 +59,15 @@ class Smarty
|
|||||||
var $global_assign = array( 'SCRIPT_NAME'
|
var $global_assign = array( 'SCRIPT_NAME'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var $cache_enable = true; // turn template cache on/off
|
||||||
|
|
||||||
|
var $cache_dir = ""; // the directory path where
|
||||||
|
// cached templates are placed.
|
||||||
|
// if empty, uses compile directory
|
||||||
|
|
||||||
|
var $cache_exp_time = 3600; // number of seconds cache is good for
|
||||||
|
|
||||||
|
|
||||||
// internal vars
|
// internal vars
|
||||||
var $_error_msg = false; // error messages
|
var $_error_msg = false; // error messages
|
||||||
var $_tpl_vars = array();
|
var $_tpl_vars = array();
|
||||||
@@ -455,7 +464,7 @@ class Smarty
|
|||||||
return $this->_compile_insert_tag($tag_args);
|
return $this->_compile_insert_tag($tag_args);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (isset($this->custom_tags[$tag_command])) {
|
if (isset($this->custom_funcs[$tag_command])) {
|
||||||
return $this->_compile_custom_tag($tag_command, $tag_args);
|
return $this->_compile_custom_tag($tag_command, $tag_args);
|
||||||
} else
|
} else
|
||||||
/* TODO syntax error: unknown tag */
|
/* TODO syntax error: unknown tag */
|
||||||
@@ -467,7 +476,7 @@ class Smarty
|
|||||||
function _compile_custom_tag($tag_command, $tag_args)
|
function _compile_custom_tag($tag_command, $tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
$function = $this->custom_tags[$tag_command];
|
$function = $this->custom_funcs[$tag_command];
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
foreach ($attrs as $arg_name => $arg_value) {
|
||||||
if (is_bool($arg_value))
|
if (is_bool($arg_value))
|
||||||
$arg_value = $arg_value ? 'true' : 'false';
|
$arg_value = $arg_value ? 'true' : 'false';
|
||||||
@@ -858,9 +867,9 @@ class Smarty
|
|||||||
|
|
||||||
function _parse_var($var_expr)
|
function _parse_var($var_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', substr($var_expr, 1));
|
$custom_mods = explode('|', substr($var_expr, 1));
|
||||||
|
|
||||||
$sections = explode('/', array_shift($modifiers));
|
$sections = explode('/', array_shift($custom_mods));
|
||||||
$var_name = array_pop($sections);
|
$var_name = array_pop($sections);
|
||||||
|
|
||||||
$output = "\$$var_name";
|
$output = "\$$var_name";
|
||||||
@@ -869,42 +878,42 @@ class Smarty
|
|||||||
$output .= "[\$_sections['$section']['properties']['index']]";
|
$output .= "[\$_sections['$section']['properties']['index']]";
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_conf_var($conf_var_expr)
|
function _parse_conf_var($conf_var_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', $conf_var_expr);
|
$custom_mods = explode('|', $conf_var_expr);
|
||||||
|
|
||||||
$var_name = substr(array_shift($modifiers), 1, -1);
|
$var_name = substr(array_shift($custom_mods), 1, -1);
|
||||||
|
|
||||||
$output = "\$_config['$var_name']";
|
$output = "\$_config['$var_name']";
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_section_prop($section_prop_expr)
|
function _parse_section_prop($section_prop_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', $section_prop_expr);
|
$custom_mods = explode('|', $section_prop_expr);
|
||||||
|
|
||||||
preg_match('!%(\w+)\.(\w+)%!', array_shift($modifiers), $match);
|
preg_match('!%(\w+)\.(\w+)%!', array_shift($custom_mods), $match);
|
||||||
$section_name = $match[1];
|
$section_name = $match[1];
|
||||||
$prop_name = $match[2];
|
$prop_name = $match[2];
|
||||||
|
|
||||||
$output = "\$_sections['$section_name']['properties']['$prop_name']";
|
$output = "\$_sections['$section_name']['properties']['$prop_name']";
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_modifiers(&$output, $modifiers)
|
function _parse_modifiers(&$output, $custom_mods)
|
||||||
{
|
{
|
||||||
foreach ($modifiers as $modifier) {
|
foreach ($custom_mods as $modifier) {
|
||||||
$modifier = explode(':', $modifier);
|
$modifier = explode(':', $modifier);
|
||||||
$modifier_name = array_shift($modifier);
|
$modifier_name = array_shift($modifier);
|
||||||
|
|
||||||
@@ -918,7 +927,7 @@ class Smarty
|
|||||||
* First we lookup the modifier function name in the registered
|
* First we lookup the modifier function name in the registered
|
||||||
* modifiers table.
|
* modifiers table.
|
||||||
*/
|
*/
|
||||||
$mod_func_name = $this->modifiers[$modifier_name];
|
$mod_func_name = $this->custom_mods[$modifier_name];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we don't find that modifier there, we assume it's just a PHP
|
* If we don't find that modifier there, we assume it's just a PHP
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<?
|
<?
|
||||||
|
|
||||||
require("Smarty.class.php");
|
require("./Smarty.class.php");
|
||||||
|
|
||||||
$smarty = new Smarty;
|
$smarty = new Smarty;
|
||||||
|
|
||||||
|
@@ -19,4 +19,4 @@ My interests are:
|
|||||||
none
|
none
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
{$Name|@count}
|
({$FirstName|@count})
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<?
|
<?
|
||||||
|
|
||||||
require("Smarty.class.php");
|
require("./Smarty.class.php");
|
||||||
|
|
||||||
$smarty = new Smarty;
|
$smarty = new Smarty;
|
||||||
|
|
||||||
|
@@ -41,11 +41,11 @@ class Smarty
|
|||||||
|
|
||||||
var $config_dir = "./configs"; // directory where config files are located
|
var $config_dir = "./configs"; // directory where config files are located
|
||||||
|
|
||||||
var $custom_tags = array( 'html_options' => 'smarty_func_html_options',
|
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
|
||||||
'html_select_date' => 'smarty_func_html_select_date'
|
'html_select_date' => 'smarty_func_html_select_date'
|
||||||
);
|
);
|
||||||
|
|
||||||
var $modifiers = array( 'lower' => 'strtolower',
|
var $custom_mods = array( 'lower' => 'strtolower',
|
||||||
'upper' => 'strtoupper',
|
'upper' => 'strtoupper',
|
||||||
'capitalize' => 'ucwords',
|
'capitalize' => 'ucwords',
|
||||||
'escape' => 'smarty_mod_escape',
|
'escape' => 'smarty_mod_escape',
|
||||||
@@ -59,6 +59,15 @@ class Smarty
|
|||||||
var $global_assign = array( 'SCRIPT_NAME'
|
var $global_assign = array( 'SCRIPT_NAME'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var $cache_enable = true; // turn template cache on/off
|
||||||
|
|
||||||
|
var $cache_dir = ""; // the directory path where
|
||||||
|
// cached templates are placed.
|
||||||
|
// if empty, uses compile directory
|
||||||
|
|
||||||
|
var $cache_exp_time = 3600; // number of seconds cache is good for
|
||||||
|
|
||||||
|
|
||||||
// internal vars
|
// internal vars
|
||||||
var $_error_msg = false; // error messages
|
var $_error_msg = false; // error messages
|
||||||
var $_tpl_vars = array();
|
var $_tpl_vars = array();
|
||||||
@@ -455,7 +464,7 @@ class Smarty
|
|||||||
return $this->_compile_insert_tag($tag_args);
|
return $this->_compile_insert_tag($tag_args);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (isset($this->custom_tags[$tag_command])) {
|
if (isset($this->custom_funcs[$tag_command])) {
|
||||||
return $this->_compile_custom_tag($tag_command, $tag_args);
|
return $this->_compile_custom_tag($tag_command, $tag_args);
|
||||||
} else
|
} else
|
||||||
/* TODO syntax error: unknown tag */
|
/* TODO syntax error: unknown tag */
|
||||||
@@ -467,7 +476,7 @@ class Smarty
|
|||||||
function _compile_custom_tag($tag_command, $tag_args)
|
function _compile_custom_tag($tag_command, $tag_args)
|
||||||
{
|
{
|
||||||
$attrs = $this->_parse_attrs($tag_args);
|
$attrs = $this->_parse_attrs($tag_args);
|
||||||
$function = $this->custom_tags[$tag_command];
|
$function = $this->custom_funcs[$tag_command];
|
||||||
foreach ($attrs as $arg_name => $arg_value) {
|
foreach ($attrs as $arg_name => $arg_value) {
|
||||||
if (is_bool($arg_value))
|
if (is_bool($arg_value))
|
||||||
$arg_value = $arg_value ? 'true' : 'false';
|
$arg_value = $arg_value ? 'true' : 'false';
|
||||||
@@ -858,9 +867,9 @@ class Smarty
|
|||||||
|
|
||||||
function _parse_var($var_expr)
|
function _parse_var($var_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', substr($var_expr, 1));
|
$custom_mods = explode('|', substr($var_expr, 1));
|
||||||
|
|
||||||
$sections = explode('/', array_shift($modifiers));
|
$sections = explode('/', array_shift($custom_mods));
|
||||||
$var_name = array_pop($sections);
|
$var_name = array_pop($sections);
|
||||||
|
|
||||||
$output = "\$$var_name";
|
$output = "\$$var_name";
|
||||||
@@ -869,42 +878,42 @@ class Smarty
|
|||||||
$output .= "[\$_sections['$section']['properties']['index']]";
|
$output .= "[\$_sections['$section']['properties']['index']]";
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_conf_var($conf_var_expr)
|
function _parse_conf_var($conf_var_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', $conf_var_expr);
|
$custom_mods = explode('|', $conf_var_expr);
|
||||||
|
|
||||||
$var_name = substr(array_shift($modifiers), 1, -1);
|
$var_name = substr(array_shift($custom_mods), 1, -1);
|
||||||
|
|
||||||
$output = "\$_config['$var_name']";
|
$output = "\$_config['$var_name']";
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_section_prop($section_prop_expr)
|
function _parse_section_prop($section_prop_expr)
|
||||||
{
|
{
|
||||||
$modifiers = explode('|', $section_prop_expr);
|
$custom_mods = explode('|', $section_prop_expr);
|
||||||
|
|
||||||
preg_match('!%(\w+)\.(\w+)%!', array_shift($modifiers), $match);
|
preg_match('!%(\w+)\.(\w+)%!', array_shift($custom_mods), $match);
|
||||||
$section_name = $match[1];
|
$section_name = $match[1];
|
||||||
$prop_name = $match[2];
|
$prop_name = $match[2];
|
||||||
|
|
||||||
$output = "\$_sections['$section_name']['properties']['$prop_name']";
|
$output = "\$_sections['$section_name']['properties']['$prop_name']";
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $custom_mods);
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parse_modifiers(&$output, $modifiers)
|
function _parse_modifiers(&$output, $custom_mods)
|
||||||
{
|
{
|
||||||
foreach ($modifiers as $modifier) {
|
foreach ($custom_mods as $modifier) {
|
||||||
$modifier = explode(':', $modifier);
|
$modifier = explode(':', $modifier);
|
||||||
$modifier_name = array_shift($modifier);
|
$modifier_name = array_shift($modifier);
|
||||||
|
|
||||||
@@ -918,7 +927,7 @@ class Smarty
|
|||||||
* First we lookup the modifier function name in the registered
|
* First we lookup the modifier function name in the registered
|
||||||
* modifiers table.
|
* modifiers table.
|
||||||
*/
|
*/
|
||||||
$mod_func_name = $this->modifiers[$modifier_name];
|
$mod_func_name = $this->custom_mods[$modifier_name];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we don't find that modifier there, we assume it's just a PHP
|
* If we don't find that modifier there, we assume it's just a PHP
|
||||||
|
@@ -19,4 +19,4 @@ My interests are:
|
|||||||
none
|
none
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
{$Name|@count}
|
({$FirstName|@count})
|
||||||
|
Reference in New Issue
Block a user