Template Lite - the faster compiling PHP template engine

Methods


append

void append(mixed $key)
void append(string $key, mixed $value[, bool merge])

Description

This is used to append values to template variables. You can pass a key/value pair, or an associative array of key/value pairs.

Arguments

Example

	EXAMPLE
	=============================
	// passing key/value pairs
	$tpl->append("Name", "Fred");
	$tpl->append("Address", $address);

	// passing an associative array
	$tpl->append(array("Name" => "Fred", "Address" => $address));
	


append_by_ref

void append_by_ref(mixed $key)
void append_by_ref(string $key, mixed $value[, bool merge])

Description

This is used to append values by reference to template variables. You can pass a key/value pair, or an associative array of key/value pairs.

Arguments

Example

	EXAMPLE
	=============================
	// passing key/value pairs
	$tpl->append_by_ref("Name", "Fred");
	$tpl->append_by_ref("Address", $address);

	// passing an associative array
	$myarray = array("Name" => "Fred", "Address" => $address)
	$tpl->append_by_ref($myarray);
	


assign

void assign(mixed $key)
void assign(string $key, mixed $value)

Description

This is used to assign values to the templates. You can pass a key/value pair, or an associative array of key/value pairs.

Example

	EXAMPLE
	=============================
	// passing key/value pairs
	$tpl->assign("Name", "Fred");
	$tpl->assign("Address", $address);

	// passing an associative array
	$tpl->assign(array("Name" => "Fred", "Address" => $address));
	


assign_by_ref

void assign_by_ref(mixed $key)
void assign_by_ref(string $key, mixed $value)

Description

This is used to assign values by reference to the templates. You can pass a key/value pair, or an associative array of key/value pairs.

Example

	EXAMPLE
	=============================
	// passing key/value pairs
	$tpl->assign_by_ref("Name", "Fred");
	$tpl->assign_by_ref("Address", $address);

	// passing an associative array
	$myarray = array("Name" => "Fred", "Address" => $address)
	$tpl->assign_by_ref($myarray);
	


assign_config

void assign_config(string $key)
void assign_config(string $key, mixed $value)

Description

This is used to assign a constant or "config" value. These constant values are referenced in the same way as values loaded from config files. Usage is the same as with assign.

Example

	EXAMPLE
	=============================
	// passing key/value pairs
	$tpl->assign("Name", "Fred");
	$tpl->assign("Address, $address);

	// passing an associative array
	$tpl->assign(array("Name" => "Fred", "Address" => $address));
	


clear_assign

void clear_assign([string $key])
void clear_assign([array $key])

Description

This will effectively erase an assigned variable.

Example

	EXAMPLE
	=============================
	// clear a single variable
	$tpl->clear_assign("Name");

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

	// clear all variables
	$tpl->clear_assign();
	


clear_all_assign

void clear_all_assign()

Description

This will effectively erase all assigned variables.

Example

	EXAMPLE
	=============================
	// clear a single variable
	$tpl->clear_all_assign();
	


clear_config

void clear_config([string $key])

Description

This will effectively erase a config value, whether assigned manually or loaded from a config file.

Example

	EXAMPLE
	=============================
	// clear a single variable
	$tpl->clear_config("Name");

	// clear a multiple variables
	$tpl->clear_config(array("Name", "Address));

	// clear all variables
	$tpl->clear_config();
	


get_template_vars

mixed get_template_vars([string $key])

Description

Will return all variables in an associative array, or a single variable named $key. This will not return variables assigned inside the template, unless the template has already been processed.

Example

	EXAMPLE
	=============================
	// get the template variable called 'foo'
	$foo = $tpl->get_template_vars('foo');

	// get all assigned template variables
	$tpl_vars = $tpl->get_template_vars();
	


get_vars_config

mixed get_vars_config([string $key])

Description

Will return all config values in an associative array, or a single config value named $key. This will not return values loaded by config_load calls embedded in a template, unless the template has already been processed.

Example

	EXAMPLE
	=============================
	// get the template variable called 'foo'
	$foo = $tpl->get_vars_config('foo');

	// get all assigned template variables
	$tpl_vars = $tpl->get_vars_config();
	


clear_compiled_tpl

void clear_compiled_tpl([string $file])

Description

This will clear out the compiled template folder, or if a file is supplied, it will clear that specific template. If no file is specified, all compiled files will be deleted.

Example

	EXAMPLE
	=============================
	// clear all compiled files
	$tpl->clear_compiled_tpl();

	// clear the compiled file for the file called "index.tpl"
	$tpl->clear_compiled_tpl("index.tpl");
	


clear_cache

void clear_cache([string $file [, string $cache_id]])

Description

This will clear out the cache, or if a file and/or a cache id is supplied, it will clear that specific template. If you have utilized cache groups, then it is possible to delete a specific group by specifying a cache id. If no file or cache id is specified, all cached files will be deleted.

Example

	EXAMPLE
	=============================
	// clear the whole cache
	$tpl->clear_cache();

	// clear all files in the "gallery" group, such as "gallery|view" and "gallery|thumbnail"
	$tpl->clear_cache(null, "gallery");

	// clear only the gallery thumbnails
	$tpl->clear_cache(null, "gallery|thumbnail");

	// clear the cache for the file called "index.tpl"
	$tpl->clear_cache("index.tpl");
	// clear the cache for the file called "index.tpl" with the cache id of "homepage"
	$tpl->clear_cache("index.tpl", "homepage");
	


clear_all_cache

void clear_all_cache()

Description

This will clear out the entire cache.

Example

	EXAMPLE
	=============================
	// clear the whole cache
	$tpl->clear_all_cache();
	


is_cached

bool is_cached(string $file [, string $cache_id])

Description

Returns true if there is a valid cache for this template. This only works if caching is to true.

Example

	EXAMPLE
	=============================
	$tpl->caching = true;

	if (!$tpl->is_cached("index.tpl", "homepage")) {
		// do expensive database calls here
	}

	$tpl->display("index.tpl", "homepage");
	


register_modifier

void register_modifier(string $modifier, callback $implementation)

Description

Use this to dynamically register a modifiery plugin. Pass the template modifier name, followed by the PHP function that implements it.

The php-function callback $implementation can either be a string containing the function name, or an array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string that contains the method name, or an array of the form array(&$class, $method) with &$class being a reference to a class and $method being a method of that class.

Example

	EXAMPLE
	=============================
	// map PHP's stripslashes function to a Template Lite modifier
	// can be referenced like this: { $var|sslash } to strip slashes from variables
	$tpl->register_modifier("sslash", "stripslashes");

	// map to a custom function
	$tpl->register_modifier("fix_string", "fix_string");

	EXAMPLE MODIFIER
	=============================
	function fix_string($string) {
		// strip slashes and then make the resulting string all lowercase
		// and then capitalize all the first letters of every word
		return ucword(strtolower(stripslashes($string)));
	}
	


unregister_modifier

void unregister_modifier(string $modifier)

Description

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

Example

	EXAMPLE
	=============================
	// don't want template designers to strip tags from elements
	$tpl->unregister_modifier("strip_tags");
	


register_function

void register_function(string $function, callback $implementation)

Description

Use this dynamically register a template function. Pass in the template function name, followed by the PHP function name that implements it.

The php-function callback $implementation can either be a string containing the function name, or an array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string that contains the method name, or an array of the form array(&$class, $method) with &$class being a reference to a class and $method being a method of that class.

Example

	EXAMPLE
	=============================
	// register the function "print_current_date" as the template function "date_now"
	// can now be used like this: { date_now } or { date_now format="l, F j, Y" }
	$tpl->register_function("date_now", "print_current_date");

	EXAMPLE FUNCTION
	=============================
	function print_current_date($params, &$tpl) {
		if(empty($params['format']))
			$format = "m/d/Y"
		return date($format, time());
	}
	


unregister_function

void unregister_function(string $function)

Description

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

Example

	EXAMPLE
	=============================
	$tpl->unregister_function("fetch");
	


register_block

void register_block(string $function, $string implementation)

Description

Use this to dynamically register a block function. Pass in the block function name, followed by the PHP function that implements it. The php-function callback $implementation can either be a string containing the function name, or an array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string that contains the method name, or an array of the form array(&$class, $method) with &$class being a reference to a class and $method being a method of that class.

Example

	EXAMPLE
	=============================
	$tpl->register_block("translate", "do_translation");

	EXAMPLE FUNCTION
	=============================
	function do_translation($params, $content, &$tpl) {
		if (isset($content)) {
			$lang = $params['lang'];
			// do something with $content
			return $translation;
		}
	}

	EXAMPLE PHP
	=============================
	{ translate lang=br }
		Hello, world!
	{ /translate }
	


unregister_block

void unregister_block(string $function)

Description

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

Example

	EXAMPLE
	=============================
	$tpl->unregister_block("translate");
	


register_compiler_function

void register_compiler_function(string $function, callback $implementation)

Description

Use this dynamically register a compiler function. Pass in the compiler function name, followed by the PHP function name that implements it.

The php-function callback $implementation can either be a string containing the function name, or an array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string that contains the method name, or an array of the form array(&$class, $method) with &$class being a reference to a class and $method being a method of that class.

Compiler functions are called only during compilation of the template. They are used for injecting PHP code or other content into the template.

Example

	EXAMPLE
	=============================

	$tpl->register_compiler_function("show_tplheader", "compiler_show_tplheader");

	EXAMPLE FUNCTION
	=============================
	function compiler_show_tplheader($arguments, &$tpl)
{
return "\necho '" . $tpl->_file . " compiled at " . date('Y-m-d H:M'). "';";
}
EXAMPLE INSERTED INTO COMPILED TEMPLATE ============================= <?php
echo 'index.tpl compiled at 2006-04-12 15:34';
?>


unregister_compiler_function

void unregister_compiler_function(string $function)

Description

Use this to dynamically unregister a compiler function. Pass in the compiler function name.

Example

	EXAMPLE
	=============================
	$tpl->unregister_compiler_function("show_tplheader");
	


register_resource

void register_resource(string $function, array resource_funcs)

Description

Use this dynamically to register a template resource. Pass in the resource name, followed by the array of PHP functions implementing it.

The required resource_funcs elements are the function-callbacks for the respective source, timestamp, secure and trusted functions of the resource.
Template Lite currently doesn't support the use of class names for resources. Do not use the 5 element resource runction array.
Resources are not supported by template caching at this time.

Example

	EXAMPLE
	=============================
	$tpl->register_resource("db", array("db_get_template",
				"db_get_timestamp",
				"db_get_secure",
				"db_get_trusted"));
	
	$tpl->display("db:index.tpl");


	EXAMPLE FUNCTION
	=============================
	function db_get_template ($tpl_name, &$tpl_source, &$object)
	{
		global $db;

		$result = $db->execute("select tpl_source from my_table where tpl_name='$tpl_name'");
		$tpl_source = $result->fields['tpl_source'];
		return true;
	}

	function db_get_timestamp($tpl_name, &$tpl_timestamp, &$object)
	{
		global $db;
		$result = $db->execute("select tpl_timestamp from my_table where tpl_name='$tpl_name'");
		$tpl_timestamp = strtotime($result->fields['tpl_timestamp']);
		return true;
	}

	function db_get_secure($tpl_name, &$object)
	{
		return true;
	}

	function db_get_trusted($tpl_name, &$object)
	{
		// not used for templates
	}
	


unregister_resource

void unregister_resource(string $function)

Description

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

Example

	EXAMPLE
	=============================
	$tpl->unregister_resource("db");
	


template_exists

bool template_exists(string $file)

Description

This function checks whether the specified template exists.


display

void display(string $file [, string $cache_id]])

Description

This function will display a template. Supply a valid template file, with an optional cache id. See the caching section for more information on caching.

It is highly recommended that you set a cache id for all templates that you are caching. You can also use an absolute path name to include template files outside the defined template directory path. If you use an absolute path it is recommended you preface the path with 'file:'. IE: 'file:/my/path/template.tpl'

Example

	EXAMPLE
	=============================
	include("class.template.php");
	$tpl = new template;
	$tpl->display("index.tpl", "homepage");

	EXAMPLE (absolute path)
	=============================
	include("class.template.php");
	$tpl = new template;
	$tpl->display("/my/path/index.tpl", "homepage");

	EXAMPLE (absolute path fiel file:)
	=============================
	include("class.template.php");
	$tpl = new template;
	$tpl->display("file:/my/path/index.tpl", "homepage");
	


fetch

string fetch(string $file, [, string $cache_id]])

Description

This function will return a string containing a template. Supply a valid template file, with an optional compile id and cache id. See the caching section for more information on caching.

It is highly recommended that you set a cache id for all templates that you are caching. You can also use an absolute path name to include template files outside the defined template directory path. If you use an absolute path it is recommended you preface the path with 'file:'. IE: 'file:/my/path/template.tpl'

Example

	EXAMPLE
	=============================
	include("class.template.php");
	$tpl = new template;
	$output = $tpl->fetch("index.tpl", "homepage");
	echo $output;

	EXAMPLE (absolute path)
	=============================
	include("class.template.php");
	$tpl = new template;
	$output = $tpl->fetch("/my/path/index.tpl", "homepage");
	echo $output;

	EXAMPLE (absolute path fiel file:)
	=============================
	include("class.template.php");
	$tpl = new template;
	$output = $tpl->fetch("file:/my/path/index.tpl", "homepage");
	echo $output;
	


config_load

bool config_load(string $file [, string $section_name [, string $var_name]])

Description

This function will load the specified config file into the template. You can specify a specific section or even a specific key to load, but otherwise, all config vars from the specified template will be loaded. Will return true if the config file was successfullyl loaded and false otherwise.

Example

	EXAMPLE
	=============================
	// load all config values
	$tpl->config_load("config.ini");

	// load all config values from the [layout] section
	$tpl->config_load("config.ini", "layout");

	// load the "font_color" key
	$tpl->config_load("config.ini", "layout", "font_color");
	


load_filter

bool load_filter(string $filtertype , string $filter_name)

Description

This function will load the specified filter type and filter. The current filter types supported are output filters. These filters process the html created by the template engine before being sent to a browser using the display function or before it is stored in a variable using the fetch function.

Example

	EXAMPLE
	=============================
	// load gzip output filter
	$tpl->load_filter("output", "gzip");
	


trigger_error

bool trigger_error(string error_msg [, int level])

Description

This function can be used to output an error message using Template Lite. The level parameter can be one of the values used for the PHP trigger_error() function. Default = E_USER_WARNING

Example

	EXAMPLE
	=============================

	$tpl->trigger_error("Couldn't find your file.", E_USER_WARNING);
	


Home