Methods append void append mixed var void append string varname mixed var void append string varname mixed var boolean 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 // passing name/value pairs $smarty->append("Name","Fred"); $smarty->append("Address",$address); // passing an associative array $smarty->append(array("city" => "Lincoln","state" => "Nebraska")); append_by_ref void append_by_ref string varname mixed var void append_by_ref string varname mixed var boolean 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 // appending name/value pairs $smarty->append_by_ref("Name",$myname); $smarty->append_by_ref("Address",$address); assign void assign mixed var void assign string varname mixed 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 // passing name/value pairs $smarty->assign("Name","Fred"); $smarty->assign("Address",$address); // passing an associative array $smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); assign_by_ref void assign_by_ref string varname mixed 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 // passing name/value pairs $smarty->assign_by_ref("Name",$myname); $smarty->assign_by_ref("Address",$address); clear_all_assign void clear_all_assign This clears the values of all assigned variables. clear_all_assign // clear all assigned variables $smarty->clear_all_assign(); clear_all_cache void clear_all_cache int 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 the entire cache $smarty->clear_all_cache(); clear_assign void clear_assign string var This clears the value of an assigned variable. This can be a single value, or an array of values. clear_assign // clear a single variable $smarty->clear_assign("Name"); // clear multiple variables $smarty->clear_assign(array("Name","Address","Zip")); clear_cache voidclear_cache stringtemplate stringcache id stringcompile id stringexpire 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 the cache for a template $smarty->clear_cache("index.tpl"); // clear the cache for a particular cache id in an multiple-cache template $smarty->clear_cache("index.tpl","CACHEID"); clear_compiled_tpl void clear_compiled_tpl string tpl_file This clears the compiled version of the specified template resource, or all compiled template files if one is not specified. This function is for advanced use only, not normally needed. clear_compiled_tpl // clear a specific template resource $smarty->clear_compiled_tpl("index.tpl"); // clear entire compile directory $smarty->clear_compiled_tpl(); clear_config voidclear_config stringvar This clears all assigned config variables. If a variable name is supplied, only that variable is cleared. clear_config // clear all assigned config variables. $smarty->clear_config(); // clear one variable $smarty->clear_config('foobar'); config_load voidconfig_load stringfile stringsection 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 // load config variables and assign them $smarty->config_load('my.conf'); // load a section $smarty->config_load('my.conf','foobar'); display voiddisplay stringtemplate stringcache_id stringcompile_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 include("Smarty.class.php"); $smarty = new Smarty; $smarty->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 // absolute filepath $smarty->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"); fetch stringfetch stringtemplate stringcache_id stringcompile_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 include("Smarty.class.php"); $smarty = new Smarty; $smarty->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_vars arrayget_config_vars stringvarname 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 loaded config template var 'foo' $foo = $smarty->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_object array get_registered_object string 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 function smarty_block_foo($params, &$smarty) { if (isset[$params['object']]) { // get reference to registered object $obj_ref =& $smarty->&get_registered_object($params['object']); // use $obj_ref is now a reference to the object } } get_template_vars arrayget_template_vars stringvarname This returns the given assigned variable value. If no parameter is given, an array of all assigned variables is returned. get_template_vars // get assigned template var 'foo' $foo = $smarty->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_cached void is_cached string 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 $smarty->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 $smarty->caching = true; if(!$smarty->is_cached("index.tpl","FrontPage")) { // do database calls, assign vars here } $smarty->display("index.tpl","FrontPage"); load_filter void load_filter string type string 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 $smarty->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_block void register_block string name string impl Use this to dynamically register block functions plugins. Pass in the block function name, followed by the PHP function name that implements it. register_block /* PHP */ $smarty->register_block("translate", "do_translation"); function do_translation ($params, $content, &$smarty) { if ($content) { $lang = $params['lang']; // do some translation with $content echo $translation; } } {* template *} {translate lang="br"} Hello, world! {/translate} register_compiler_function void register_compiler_function string name string impl Use this to dynamically register a compiler function plugin. Pass in the compiler function name, followed by the PHP function that implements it. register_function void register_function string name string impl Use this to dynamically register template function plugins. Pass in the template function name, followed by the PHP function name that implements it. register_function $smarty->register_function("date_now", "print_current_date"); function print_current_date ($params) { extract($params); if(empty($format)) $format="%b %e, %Y"; echo strftime($format,time()); } // now you can use this in Smarty to print the current date: {date_now} // or, {date_now format="%Y/%m/%d"} to format it. register_modifier void register_modifier string name string impl Use this to dynamically register modifier plugin. Pass in the template modifier name, followed by the PHP function that it implements it. register_modifier // let's map PHP's stripslashes function to a Smarty modifier. $smarty->register_modifier("sslash","stripslashes"); // now you can use {$var|sslash} to strip slashes from variables register_object void register_object string object_name object $object array allowed methods/properties boolean format This is to register an object for use in the templates. See the object section of the manual for examples. register_outputfilter void register_outputfilter string function_name 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. register_postfilter void register_postfilter string function_name 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. register_prefilter void register_prefilter string function_name 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. register_resource void register_resource string name array 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. register_resource $smarty->register_resource("db", array("db_get_template", "db_get_timestamp", "db_get_secure", "db_get_trusted")); trigger_error void trigger_error string 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_exists bool template_exists string 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_block void unregister_block string name Use this to dynamically unregister block function plugin. Pass in the block function name. unregister_compiler_function void unregister_compiler_function string name Use this to dynamically unregister a compiler function. Pass in the name of the compiler function. unregister_function void unregister_function string name Use this to dynamically unregister template function plugin. Pass in the template function name. unregister_function // we don't want template designers to have access to system files $smarty->unregister_function("fetch"); unregister_modifier void unregister_modifier string name Use this to dynamically unregister modifier plugin. Pass in the template modifier name. unregister_modifier // we don't want template designers to strip tags from elements $smarty->unregister_modifier("strip_tags"); unregister_object void unregister_object string object_name Use this to unregister an object. unregister_outputfilter void unregister_outputfilter string function_name Use this to dynamically unregister an output filter. unregister_postfilter void unregister_postfilter string function_name Use this to dynamically unregister a postfilter. unregister_prefilter void unregister_prefilter string function_name Use this to dynamically unregister a prefilter. unregister_resource void unregister_resource string name Use this to dynamically unregister a resource plugin. Pass in the name of the resource. unregister_resource $smarty->unregister_resource("db");