Methods

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.

Example 6-1. assign

// passing name/value pairs
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);

// passing an associative array
$smarty->assign(array("city" => "Lincoln","state" => "Nebraska"));

append

void append(mixed var)

void append(string varname, mixed var)

This is used to append data to variables in the template. You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs.

Example 6-2. append

// passing name/value pairs
$smarty->append("Name","Fred");
$smarty->append("Address",$address);

// passing an associative array
$smarty->append(array("city" => "Lincoln","state" => "Nebraska"));

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. Array functionality was added to Smarty 1.3.0.

Example 6-3. clear_assign

// clear a single variable
$smarty->clear_assign("Name");

// clear multiple variables
$smarty->clear_assign(array("Name","Address","Zip"));

clear_all_assign

void clear_all_assign()

This clears the values of all assigned variables.

Example 6-4. clear_all_assign

// clear all assigned variables
$smarty->clear_all_assign();

clear_cache

void clear_cache(string template, string cache id)

This clears the cache for the specified template. If you have multiple caches for this template, you can clear a specific cache by supplying the cache id as the second parameter. See the caching section for more information. This was added to Smarty 1.3.0.

Example 6-5. 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_all_cache

void clear_all_cache()

This clears the entire template cache. This was added to Smarty 1.3.0.

Example 6-6. clear_all_cache

// clear the entire cache
$smarty->clear_all_cache();

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.

Example 6-7. clear_compiled_tpl

// clear a specific template resource
$smarty->clear_compiled_tpl("index.tpl");

// clear entire compile directory
$smarty->clear_compiled_tpl();

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.

Example 6-8. 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.

unregister_function

void unregister_function(string name)

Use this to dynamically unregister template function plugin. Pass in the template function name.

Example 6-9. unregister_function

// we don't want template designers to have access to system files

$smarty->unregister_function("fetch");

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.

Example 6-10. 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

unregister_modifier

void unregister_modifier(string name)

Use this to dynamically unregister modifier plugin. Pass in the template modifier name.

Example 6-11. unregister_modifier

// we don't want template designers to strip tags from elements

$smarty->unregister_modifier("strip_tags");

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.

Example 6-12. register_resource

$smarty->register_resource("db", array("db_get_template",
                                       "db_get_timestamp",
                                       "db_get_secure",
                                       "db_get_trusted"));

unregister_resource

void unregister_resource(string name)

Use this to dynamically unregister a resource plugin. Pass in the name of the resource.

Example 6-13. unregister_resource

$smarty->unregister_resource("db");

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.

unregister_prefilter

void unregister_prefilter(string function_name)

Use this to dynamically unregister a prefilter.

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.

unregister_postfilter

void unregister_postfilter(string function_name)

Use this to dynamically unregister a postfilter.

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.

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.

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. This function was added to Smarty 2.0.

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. This was added to Smarty 1.3.0.

Example 6-14. 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 an optional second parameter in case you want multiple caches for the given template.

Example 6-15. 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");

get_template_vars

array get_template_vars()

This gets an array of the currently assigned template vars.

Example 6-16. get_template_vars

// get all assigned template vars
$tpl_vars = $smarty->get_template_vars();

// take a look at them
var_dump($tpl_vars);

display

void display(string template [, string cache_id [, string compile_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. This was added to Smarty 1.4.5.

Example 6-17. 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.

Example 6-18. 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

string fetch(string template [, string cache_id [, string compile_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. This was added to Smarty 1.4.5.

Example 6-19. 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;