Flat-Frog - the faster compiling PHP template engine

Methods


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_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

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

Description

This will effectively erase an assigned variable.

Example

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

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

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


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_vars

mixed get_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_vars('foo');

	// get all assigned template variables
	$tpl_vars = $tpl->get_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

void clear_compiled([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();

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


clear_cached

void clear_cached([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_cached();

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

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

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


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 Flat-Frog 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");
	


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.

Example

	EXAMPLE
	=============================
	include("class.template.php");
	$tpl = new template;
	$tpl->display("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.

Example

	EXAMPLE
	=============================
	include("class.template.php");
	$tpl = new template;
	$output = $tpl->fetch("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");
	


Home