Methodsappendvoid appendmixed varvoid appendstring varnamemixed varvoid appendstring varnamemixed varboolean merge
This is used to append an element to an assigned array. If you append
to a string value, it is converted to an array value and then
appended to. You can explicitly pass name/value pairs, or associative
arrays containing the name/value pairs. If you pass the optional third
parameter of true, the value will be merged with the current array
instead of appended.
Technical Note
The merge parameter respects array keys, so if you merge two
numerically indexed arrays, they may overwrite each other or result in
non-sequential keys. This is unlike the array_merge() function of PHP
which wipes out numerical keys and renumbers them.
append
append("Name","Fred");
$smarty->append("Address",$address);
// passing an associative array
$smarty->append(array("city" => "Lincoln","state" => "Nebraska"));
?>
]]>
append_by_refvoid append_by_refstring varnamemixed varvoid append_by_refstring varnamemixed varboolean merge
This is used to append values to the templates by reference.
If you append a variable by reference then change its
value, the appended value sees the change as well. For objects,
append_by_ref() also avoids an in-memory copy of the appended object.
See the PHP manual on variable referencing for an in-depth
explanation. If you pass the optional third parameter of true,
the value will be merged with the current array instead of appended.
Technical Note
The merge parameter respects array keys, so if you merge two
numerically indexed arrays, they may overwrite each other or result in
non-sequential keys. This is unlike the array_merge() function of PHP
which wipes out numerical keys and renumbers them.
append_by_ref
append_by_ref("Name", $myname);
$smarty->append_by_ref("Address", $address);
?>
]]>
assignvoid assignmixed varvoid assignstring varnamemixed var
This is used to assign values to the templates. You can
explicitly pass name/value pairs, or associative arrays
containing the name/value pairs.
assign
assign('Name', 'Fred');
$smarty->assign('Address', $address);
// passing an associative array
$smarty->assign(array("city" => "Lincoln", "state" => "Nebraska"));
?>
]]>
assign_by_refvoid assign_by_refstring varnamemixed var
This is used to assign values to the templates by reference instead of
making a copy. See the PHP manual on variable referencing for an explanation.
Technical Note
This is used to assign values to the templates by reference.
If you assign a variable by reference then change its
value, the assigned value sees the change as well. For objects,
assign_by_ref() also avoids an in-memory copy of the assigned object.
See the PHP manual on variable referencing for an in-depth
explanation.
assign_by_ref
assign_by_ref('Name', $myname);
$smarty->assign_by_ref('Address', $address);
?>
]]>
clear_all_assignvoid clear_all_assign
This clears the values of all assigned variables.
clear_all_assign
clear_all_assign();
?>
]]>
clear_all_cachevoid clear_all_cacheint expire time
This clears the entire template cache. As an optional
parameter, you can supply a minimum age in seconds the cache
files must be before they will get cleared.
clear_all_cache
clear_all_cache();
?>
]]>
clear_assignvoid clear_assignstring var
This clears the value of an assigned variable. This
can be a single value, or an array of values.
clear_assign
clear_assign("Name");
// clear multiple variables
$smarty->clear_assign(array("Name","Address","Zip"));
?>
]]>
clear_cachevoidclear_cachestringtemplatestringcache idstringcompile idstringexpire time
This clears the cache for a specific template. If you have
multiple caches for this template, you can clear a specific
cache by supplying the cache id as the second parameter. You
can also pass a compile id as a third parameter. You can "group"
templates together so they can be removed as a group. See the
caching section for more
information. As an optional fourth parameter, you can supply a
minimum age in seconds the cache file must be before it will
get cleared.
clear_cache
clear_cache("index.tpl");
// clear the cache for a particular cache id in an multiple-cache template
$smarty->clear_cache("index.tpl","CACHEID");
?>
]]>
clear_compiled_tplvoidclear_compiled_tplstringtpl_filestringcompile_idstringexp_time
This clears the compiled version of the specified template
resource, or all compiled template files if one is not specified.
if you pass a compile_id only the compiled template for this
specific compile_id is cleared. if you pass an exp_time, then only
compiled templates older than exp_time seconds are cleared, by
default all compiled templates are cleared regardless of their age.
This function is for advanced use only, not normally needed.
clear_compiled_tpl
clear_compiled_tpl("index.tpl");
// clear entire compile directory
$smarty->clear_compiled_tpl();
?>
]]>
clear_configvoidclear_configstringvar
This clears all assigned config variables. If a variable name is
supplied, only that variable is cleared.
clear_config
clear_config();
// clear one variable
$smarty->clear_config('foobar');
?>
]]>
config_loadvoidconfig_loadstringfilestringsection
This loads config file data and assigns it to the template. This
works identical to the template config_load
function.
Technical Note
As of Smarty 2.4.0, assigned template variables are kept across
invocations of fetch() and display(). Config vars loaded from
config_load() are always global scope. Config files are also
compiled for faster execution, and respect the force_compile and compile_check settings.
config_load
config_load('my.conf');
// load a section
$smarty->config_load('my.conf','foobar');
?>
]]>
displayvoiddisplaystringtemplatestringcache_idstringcompile_id
This displays the template. Supply a valid template resource
type and path. As an optional second parameter, you can pass a
cache id. See the caching
section for more information.
As an optional third parameter, you can pass a compile id. This
is in the event that you want to compile different versions of
the same template, such as having separate templates compiled
for different languages. Another use for compile_id is when you
use more than one $template_dir but only one $compile_dir. Set
a separate compile_id for each $template_dir, otherwise
templates of the same name will overwrite each other. You can
also set the $compile_id variable once
instead of passing this to each call to display().
display
caching = true;
// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl"))
{
// dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" => "68502"
);
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data);
}
// display the output
$smarty->display("index.tpl");
?>
]]>
Use the syntax for template resources to
display files outside of the $template_dir directory.
function display template resource examples
display("/usr/local/include/templates/header.tpl");
// absolute filepath (same thing)
$smarty->display("file:/usr/local/include/templates/header.tpl");
// windows absolute filepath (MUST use "file:" prefix)
$smarty->display("file:C:/www/pub/templates/header.tpl");
// include from template resource named "db"
$smarty->display("db:header.tpl");
?>
]]>
fetchstringfetchstringtemplatestringcache_idstringcompile_id
This returns the template output instead of displaying it.
Supply a valid template resource
type and path. As an optional second parameter, you can pass a
cache id. See the caching
section for more information.
As an optional third parameter, you can pass a compile id. This
is in the event that you want to compile different versions of
the same template, such as having separate templates compiled
for different languages. Another use for compile_id is when you
use more than one $template_dir but only one $compile_dir. Set
a separate compile_id for each $template_dir, otherwise
templates of the same name will overwrite each other. You can
also set the $compile_id variable once
instead of passing this to each call to fetch().
fetch
caching = true;
// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl"))
{
// dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" => "68502"
);
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data);
}
// capture the output
$output = $smarty->fetch("index.tpl");
// do something with $output here
echo $output;
?>
]]>
get_config_varsarrayget_config_varsstringvarname
This returns the given loaded config variable value. If no parameter
is given, an array of all loaded config variables is returned.
get_config_vars
get_config_vars('foo');
// get all loaded config template vars
$config_vars = $smarty->get_config_vars();
// take a look at them
print_r($config_vars);
?>
]]>
get_registered_objectarray get_registered_objectstring object_name
This returns a reference to a registered object. This is useful
from within a custom function when you need direct access to a
registered object.
get_registered_object
get_registered_object($params['object']);
// use $obj_ref is now a reference to the object
}
}
?>
]]>
get_template_varsarrayget_template_varsstringvarname
This returns the given assigned variable value. If no parameter
is given, an array of all assigned variables is returned.
get_template_vars
get_template_vars('foo');
// get all assigned template vars
$tpl_vars = $smarty->get_template_vars();
// take a look at them
print_r($tpl_vars);
?>
]]>
is_cachedboolean is_cachedstring template[string cache_id]
This returns true if there is a valid cache for this template.
This only works if caching is set to true.
is_cached
caching = true;
if(!$smarty->is_cached("index.tpl")) {
// do database calls, assign vars here
}
$smarty->display("index.tpl");
?>
]]>
You can also pass a cache id as an optional second parameter
in case you want multiple caches for the given template.
is_cached with multiple-cache template
caching = true;
if(!$smarty->is_cached("index.tpl","FrontPage")) {
// do database calls, assign vars here
}
$smarty->display("index.tpl","FrontPage");
?>
]]>
load_filtervoid load_filterstring typestring name
This function can be used to load a filter plugin. The first
argument specifies the type of the filter to load and can be one
of the following: 'pre', 'post', or 'output'. The second argument
specifies the name of the filter plugin, for example, 'trim'.
loading filter plugins
load_filter('pre', 'trim'); // load prefilter named 'trim'
$smarty->load_filter('pre', 'datefooter'); // load another prefilter named 'datefooter'
$smarty->load_filter('output', 'compress'); // load output filter named 'compress'
?>
]]>
register_blockvoid register_blockstring namemixed implbool cacheablearray or null cache_attrs
Use this to dynamically register block functions plugins.
Pass in the block function name, followed by the PHP
function callback that implements it.
The php-function callback impl can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
$cacheable and $cache_attrs can be omitted in most cases. See Controlling Cacheability of Plugins' Output on how to use them properly.
register_block
register_block("translate", "do_translation");
function do_translation ($params, $content, &$smarty, &$repeat) {
if (isset($content)) {
$lang = $params['lang'];
// do some translation with $content
return $translation;
}
}
?>
{* template *}
{translate lang="br"}
Hello, world!
{/translate}
]]>
register_compiler_functionvoid register_compiler_functionstring namemixed implbool cacheable
Use this to dynamically register a compiler function plugin.
Pass in the compiler function name, followed by the PHP
function that implements it.
The php-function callback impl can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
$cacheable can be omitted in
most cases. See Controlling
Cacheability of Plugins' Output on how to it properly.
register_functionvoid register_functionstring namemixed implbool cacheablearray or null cache_attrs
Use this to dynamically register template function plugins.
Pass in the template function name, followed by the PHP
function name that implements it.
The php-function callback impl can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
$cacheable and $cache_attrs can be omitted in most cases. See Controlling Cacheability of Plugins' Output on how to use them properly.
register_function
register_function("date_now", "print_current_date");
function print_current_date ($params) {
if(empty($params['format']))
$format = "%b %e, %Y";
else
$format = $params['format'];
return strftime($format,time());
}
// now you can use this in Smarty to print the current date: {date_now}
// or, {date_now format="%Y/%m/%d"} to format it.
?>
]]>
register_modifiervoid register_modifierstring namemixed impl
Use this to dynamically register modifier plugin. Pass in the
template modifier name, followed by the PHP function that it
implements it.
The php-function callback impl can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
register_modifier
register_modifier("sslash","stripslashes");
// now you can use {$var|sslash} to strip slashes from variables
?>
]]>
register_objectvoid register_objectstring object_nameobject $objectarray allowed methods/propertiesboolean formatarray block methods
This is to register an object for use in the templates. See the
object section
of the manual for examples.
register_outputfiltervoid register_outputfiltermixed function
Use this to dynamically register outputfilters to operate on
a template's output before it is displayed. See
template output
filters
for more information on how to set up an output filter function.
The php-function callback function can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
register_postfiltervoid register_postfiltermixed function
Use this to dynamically register postfilters to run templates
through after they are compiled. See template postfilters for
more information on how to setup a postfiltering function.
The php-function callback function can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
register_prefiltervoid register_prefiltermixed function
Use this to dynamically register prefilters to run templates
through before they are compiled. See template prefilters for
more information on how to setup a prefiltering function.
The php-function callback function can be either (a) a string
containing the function name or (b) an array of the form
array(&$object, $method) with
&$object being a reference to an
object and $method being a string
containing the mehod-name or (c) an array of the form
array(&$class, $method) with
$class being a classname and
$method being a class method of that
class.
register_resourcevoid register_resourcestring namearray resource_funcs
Use this to dynamically register a resource plugin with Smarty.
Pass in the name of the resource and the array of PHP functions
implementing it. See
template resources
for more information on how to setup a function for fetching
templates.
Technical Note
A resource name must be at least two characters in length. One
character resource names will be ignored and used as part of the file
path, such as $smarty->display('c:/path/to/index.tpl');
The php-function-array resource_funcs
must have 4 or 5 elements. With 4 elements the elements are
the functions-callbacks for the respective "source",
"timestamp", "secure" and "trusted" functions of the
resource. With 5 elements the first element has to be an
object reference or a class name of the object or class
implementing the resource and the 4 following elements have
to be the method names implementing "source", "timestamp",
"secure" and "trusted".
register_resource
register_resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
?>
]]>
trigger_errorvoid trigger_errorstring error_msg[int level]
This function can be used to output an error message using Smarty.
level parameter can be one of the values
used for trigger_error() PHP function, i.e. E_USER_NOTICE,
E_USER_WARNING, etc. By default it's E_USER_WARNING.
template_existsbool template_existsstring template
This function checks whether the specified template exists. It can
accept either a path to the template on the filesystem or a
resource string specifying the template.
unregister_blockvoid unregister_blockstring name
Use this to dynamically unregister block function plugin.
Pass in the block function name.
unregister_compiler_functionvoid unregister_compiler_functionstring name
Use this to dynamically unregister a compiler function. Pass in
the name of the compiler function.
unregister_functionvoid unregister_functionstring name
Use this to dynamically unregister template function plugin.
Pass in the template function name.
unregister_function
unregister_function("fetch");
?>
]]>
unregister_modifiervoid unregister_modifierstring name
Use this to dynamically unregister modifier plugin. Pass in the
template modifier name.
unregister_modifier
unregister_modifier("strip_tags");
?>
]]>
unregister_objectvoid unregister_objectstring object_name
Use this to unregister an object.
unregister_outputfiltervoid unregister_outputfilterstring function_name
Use this to dynamically unregister an output filter.
unregister_postfiltervoid unregister_postfilterstring function_name
Use this to dynamically unregister a postfilter.
unregister_prefiltervoid unregister_prefilterstring function_name
Use this to dynamically unregister a prefilter.
unregister_resourcevoid unregister_resourcestring name
Use this to dynamically unregister a resource plugin. Pass in the
name of the resource.
unregister_resource
unregister_resource("db");
?>
]]>