Template Lite - the faster compiling PHP template engine

Functions


assign

Description

This function will create a new variable from the template and assign a value to it.

Arguments

Example

	TEMPLATE
	=============================
	{ assign var="test" value="this is a test variable"|upper }
	The value of $test is { $test }.

	OUTPUT
	=============================
	The value of $test is THIS IS A TEST VARIABLE.
	


config_load

Description

This function will load a config file into a template.

Arguments

Example

	EXAMPLE
	=============================
	{ config_load file="config.conf" }
	{ #variable# }
	


foreach/foreachelse

Description

Much as the name describes, this function behaves exactly like foreach. It will loop through an array and return the output.

Arguments

Example

	PHP
	=============================
	$tpl->assign("contacts", array(
		array("phone" => "1", "fax" => "2", "cell" => "3"),
		array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")
	));

	TEMPLATE
	=============================
	{ foreach value=contact from=$contacts }
		{ foreach key=key value=item from=$contact }
			{ $key }: { $item }<br>
		{ /foreach }
	{ /foreach }

	OUTPUT
	=============================
	phone: 1<br>
	fax: 2<br>
	cell: 3<br>
	phone: 555-4444<br>
	fax: 555-3333<br>
	cell: 760-1234<br>
	


for

Description

This function will loop through a block the specific number of times. It is very similar to PHP's for. You can specify a starting integer, an ending integer, and a step value.

Arguments

Example

	EXAMPLE
	=============================
	{ for start=0 stop=10 step=2 value=current }
	We are on number { $current }
	{ /for }

	{ for start=0 stop=1 step=0.2 value=current }
	We are on number { $current }
	{ /for }

	OUTPUT
	=============================
	We are on number 0
	We are on number 2
	We are on number 4
	We are on number 6
	We are on number 8

	We are on number 0
	We are on number 0.2
	We are on number 0.4
	We are on number 0.6
	We are on number 0.8
	


include

Description

This function will process another template and include the results in the original template. Alternatively, it will process another template and store the results in a variable for use in the template that called the include to begin with. Included files are also compiled and optionally saved for faster execution. You can also use an absolute path name to include template files outside the defined template directory path.

Arguments

Example

	EXAMPLE
	=============================
	{ include file="template.tpl" }
	{ include file=$page }
	{ include file="/usr/local/include/templates/header.tpl" }
	{ include file="file:/usr/local/include/templates/header.tpl" }
	


insert

Description

This function will insert text into a template. The text is generated by calling a matching function created by the programmer in PHP that will create necessary text, such as returning compiled template information, or other information for inclusion in the calling template. The output of an insert is not cached and thus an insert is called everytime a template is loaded.

Arguments

Example

	PHP
	=============================
	function insert_stuffandjunk($params, &$tpl) {
		return $tpl->fetch('template.tpl','sidebar|template');
	}

	function insert_othercrap($params, &$tpl) {
		return "random text: " . $params["var"];
	}

	TEMPLATE
	=============================
	{ insert name="stuffandjunk" }
	{ insert name="othercrap" var="hi" }

	OUTPUT
	=============================
	This is the contents of template.tpl
	random text: hi
	


if/elseif/else

Description

Much as the name describes, this is the equivalent to PHP's if statement. In fact, it offers exactly the same flexibility, if not more. Every if, though, must be paired with an /if. Additionally, else and elseif are permitted. The comparison tokens allowed are !, %, !==, ==, ===, >, <, !=, <>, <<, >>, <=, >=,&&, ||, ^, &, ~, (, ), ,, +, -, *, /, @, eq, ne, neq, lt, le, lte, gt, ge, gte, and, or, not, mod. It is important to note that the comparison operators must be separated from the variables by at least one space.

NEW! The is token is now supported. The following expressions can be used. Expressions in brackets [] are optional.

is [not] div by
is [not] even [by]
is [not] odd [by]

Example

	EXAMPLES
	=============================
	{if $name eq "Fred"}
		Welcome Sir.
	{elseif $name eq "Wilma"}
		Welcome Ma'am.
	{else}
		Welcome, whatever you are.
	{/if}

	{* an example with "or" logic *}
	{if $name eq "Fred" or $name eq "Wilma"}
		...
	{/if}

	{* same as above *}
	{if $name == "Fred" || $name == "Wilma"}
		...
	{/if}

	{* the following syntax will NOT work, conditional qualifiers
	   must be separated from surrounding elements by spaces *}
	{if $name=="Fred" || $name=="Wilma"}
		...
	{/if}

	{* parenthesis are allowed *}
	{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
		...
	{/if}

	{* you can also embed php function calls *}
	{if count($var) gt 0}
		...
	{/if}

	{ assign var="var" value=2 }
	{if $var is even}
		Yes it's even.
	{/if}

	{ assign var="var" value=3 }
	{if $var is odd}
		Yes it's odd.
	{/if}

	{ assign var="var" value=2 }
	{if $var is not odd}
		No not odd.
	{/if}

	{ assign var="var" value=8 }
	{if $var is div by 4}
		Yes it is divisible by 4.
	{/if}

	{ assign var="var" value=8 }
	{if $var is even by 2}
		Yes it is even by 2.
	{/if}

	{ assign var="var" value=6 }
	{if $var is even by 3}
	 Yes it is even by 3.
	{/if}

	


ldelim/rdelim

Description

These will insert the user-defined left delimiter and right delimiter into the template.

Example

	TEMPLATE
	=============================
	Here is an example: { ldelim } config_load file="config.conf" { rdelim }

	OUTPUT
	=============================
	Here is an example: { config_load file="config.conf" }
	


literal

Description

This block function will take everything inside it and interpret it literally. This means that you can put template code inside of this function and it will not be parsed.

Example

	EXAMPLE
	=============================
	{ literal }
		Here is an example: { config_load file="config.conf" }
	{ /literal }
	


php

Description

This block function will take everything inside it and execute it as pure PHP code. Everything in this block gets sent directly to the PHP compiler. To access variables from the template engine from inside a php block, use $this->get_vars(varname).

Example

	EXAMPLE
	=============================
	{ php }
		echo "Hello to " . $this->get_vars('username') . "<BR>";
	{ /php }
	


section/sectionelse

Description

This function will loop through a block a specific number of times similar to the foreach function. All {section} tags must be paired with {/section} tags. Required parameters are name and loop. A {sectionelse} is executed when there are no values in the loop variable.

Arguments for section

Properties for section

Example

	PHP
	=============================
	$player_id = array(1,2,3);
	$tpl->assign('player_id',$player_id);

	$player_name = array('Panama Jack','Tarnus Harten','Goober');
	$tpl->assign('player_name',$player_name);

	TEMPLATE
	=============================
	{section name=player_number loop=$player_id}
	<p>
	  Player ID: {$player_id[player_number]}<br />
	  Player Name: {$player_name[player_number]}
	</p>
	{/section}

	OUTPUT
	=============================
	<p>
	  Player ID: 1<br />
	  Player Name: Panama Jack
	</p>
	<p>
	  Player ID: 2<br />
	  Player Name: Tarnus Harten
	</p>
	<p>
	  Player ID: 3<br />
	  Player Name: Goober
	</p>
	


switch/case

Description

Much like the name indicates, this function behaves very similar to PHP's switch function. It will execute a different block of code depending on the given value.

Arguments for switch

Arguments for case

Example

	TEMPLATE
	=============================
	{ switch from=$variable }
		{ case value="case1" }
			This is case number one.
		{ case value="case2" }
			This is case number 2
		{ case }
			This is the default. Nothing matched above.
	{ /switch }
	


while

Description

Much like the name indicates, this function behaves very similar to PHP's while function. It will execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.

The while function operates similar to the if statement. It offers exactly the same flexibility while looping through the nested statements(s). Every while must be paired with an /while. The comparison tokens allowed are !, %, !==, ==, ===, >, <, !=, <>, <<, >>, <=, >=,&&, ||, ^, &, ~, (, ), ,, +, -, *, /, @, eq, ne, neq, lt, le, lte, gt, ge, gte, and, or, not, mod. It is important to note that the comparison operators must be separated from the variables by at least one space.

Arguments

Example

	TEMPLATE
	=============================
	{assign var="counter" value="0"}
	{while $counter < 10}
		[{$counter}] 
{ math equation="x + 1" x=$counter assign="counter" } {/while} OUTPUT ============================= [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]


Home