Template Lite - the faster compiling PHP template engine

Variables


Variables assigned from PHP

Basic Variables

To reference a variable in the template, the syntax is similar to a reference from PHP. All variables in the template are completely seperate from the PHP code and variables in your PHP code cannot be seen from the template. Here is are some examples of how to set various variables in PHP and refer to them in Template Lite. These examples all assume that you have instanitiated the template class and are referring to it as $tpl.
	PHP
	=============================
	$tpl->assign("foo","bar");

	TEMPLATE
	=============================
	{ $foo }

	OUTPUT
	=============================
	bar
	

Arrays

As was the case with basic variables, arrays are referenced in templates using a period (.) as a delimiter between each key. You have the name of the variable followed by a period (.) containing the key of the array value that you want to view. If you are using a numeric key or a variable you can use brackets [] around the numeric element instead of a period (.). Do not use brackets around a fixed text key as this will reference a Section Array variable.
	PHP
	=============================
	$foo = array("apples", "oranges", "bananas");
	$tpl->assign("foo", $foo);

	TEMPLATE
	=============================
	{ $foo }
	{ $foo[0] }
	{ $foo.2 }
	{ assign var=key value=2 }
	{ $foo[$key] }
	{ $foo.$key }

	OUTPUT
	=============================
	Array
	apples
	bananas
	oranges
	oranges
	

Associative Arrays

As was the case with basic variables, arrays are referenced in templates using a period (.) as a delimiter between each key. You have the name of the variable followed by a period (.) containing the key of the array value that you want to view.

Note that the array indeces do NOT have quotes around them. If you use quotes around the index, an error will not be returned, but you will not get the results that you expected.
	PHP
	=============================
	$foo = array("fruit" => "apples", "vegetable" => "carrot", "dairy" => "milk");
	$tpl->assign("foo", $foo);

	TEMPLATE
	=============================
	{ $foo.fruit }
	{ $foo.dairy }
	{ $foo.vegetable }

	OUTPUT
	=============================
	apples
	milk
	carrot
	

Section Arrays

Section Array references uses similar syntax to PHP arrays. If you use any text within brackets on an array you will reference a Section Loop variable. You should NOT use brackets to reference a normal array as you would with a PHP variable.
	PHP
	=============================
	$foo = array("apples", "carrot", "milk");
	$tpl->assign("foo", $foo);


	TEMPLATE
	=============================
	{section name=item loop=$foo}
		id: {$foo[item]}
	{/section}
	
	OUTPUT
	=============================
	apples
	carrot
	milk
	

Variables loaded from config files

After you have loaded a config file, you might want to reference the variables you have loaded from your template. Config variables are more like static constants in that they cannot be changed from the template and are thus referred to differently than normal variables. They are offset by hash or pound marks (#) on either side of the variable.

	CONFIG FILE (config.ini)
	=============================
	foo = "bar"
	test = "this is a string"

	TEMPLATE
	=============================
	{ config_load file="config.ini" }
	{ #foo# }

	OUTPUT
	=============================
	bar
	


Embedding variables in quotes

Variables in quotes are handled exactly the same in Smarty as they are in PHP. Here are some examples:

	{ "test $foo test" }		<- sees $foo
	{ "test $foo_bar test" }	<- sees $foo_bar
	{ "test $foo[0] test" }		<- sees $foo[0]
	{ "test $foo[bar] test" }	<- sees $foo[bar]
	{ "test $foo.bar test" }	<- sees $foo[bar]
	{ "test `$foo.bar` test" }	<- sees $foo
	{ 'test $foo test' }		<- will interpret literally
	


Variable concatenation

The are two ways to concatenate variables using Template Lite. The first is using the standard PHP-compliant 'dot' syntax. The other is to use a comma syntax. Here are some examples of each way.

	PHP
	=============================
	$tpl->assign("foo","bar");
	$tpl->assign("name","Paul");

	TEMPLATE
	=============================
	{ $foo.$name }
	{ $foo." ".$name }
	{ $foo, $name }
	{ $foo, " ", $name }

	OUTPUT
	=============================
	barPaul
	bar Paul
	barPaul
	bar Paul
	
The comma concatenation technique works only as a standalone tag, i.e. { $foo, $bar } but not { func arg=$foo, $bar }, whereas the 'dot' syntax works in all cases where a variable is accepted.


$templatelite reserved variable

Using the $templatelite variable, you can refer to special environment variables from inside the template.
If you are using the $smarty variable in any of your templates you should change it to $templatelite.

If you would like to maintain compatibility with Smarty you can change the name of the reserved variable to smarty using the "reserved_template_varname" configuration variable.

	EXAMPLE
	=============================
	$template_object = new Template_Lite; 
	$template_object->template_dir = "./templates/"; 
	$template_object->compile_dir = "./templates_c/"; 
	$template_object->reserved_template_varname = "smarty"; 
	
With the above change your templates would use the $smarty variable.
	{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *}
	{ $templatelite[GET][PAGE] }
	{ $templatelite.GET.PAGE }
	

CAPTURE reserved variable

	{* The output captured using the {capture}..{/capture} function *}
	{* can be accessed using the {$templatelite} variable. *}
	{* name = the name of the captured block *}
	{ $templatelite[CAPTURE][name] }
	{ $templatelite.CAPTURE.name }
	

CONFIG reserved variable

	{* The {$templatelite} variable can be used to refer to loaded *}
	{* config variables. {$templatelite.config.name} is identical to {#name#}. *}
	{* Examples: {config_load}. *}
	{* name = the name of the config variable to reference *}
	{ $templatelite[CONFIG][name] }
	{ $templatelite.CONFIG.name }
	

CONST reserved variable

	{* The {$templatelite} variable can be used to refer to loaded *}
	{* defined constants. *}
	{* name = the name of the defined constant to reference *}
	{ $templatelite[CONST][name] }
	{ $templatelite.CONST.name }
	

COOKIE reserved variable

	{* display the value of the cookie "username" *}
	{ $templatelite[COOKIE][username] }
	{ $templatelite.COOKIE.username }
	

ENV reserved variable

	{* display the system environment variable "PATH" *}
	{ $templatelite[ENV][PATH] }
	{ $templatelite.ENV.PATH }
	

GET reserved variable

	{* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *}
	{ $templatelite[GET][PAGE] }
	{ $templatelite.GET.PAGE }
	

LDELIM reserved variable

	{* Display the left-delimiter value. *}
	{ $templatelite[LDELIM] }
	{ $templatelite.LDELIM }
	

NOW reserved variable

	{* display the current time in unix epoch form *}
	{ $templatelite[NOW] }
	{ $templatelite.NOW }
	

POST reserved variable

	{* display the variable "page" from a form (POST) *}
	{ $templatelite[POST][PAGE] }
	{ $templatelite.POST.PAGE }
	

RDELIM reserved variable

	{* Display the right-delimiter value. *}
	{ $templatelite[RDELIM] }
	{ $templatelite.RDELIM }
	

SECTION reserved variable

	{* The {$template} variable can be used to refer to one of *}
	{* many {section} loop properties *}
	{* ??? = the name of a property *}
	{ $templatelite[SECTION][???] }
	{ $templatelite.SECTION.??? }
	

SERVER reserved variable

	{* display the server variable "SERVER_NAME" *}
	{ $templatelite[SERVER][SERVER_NAME] }
	{ $templatelite.SERVER.SERVER_NAME }
	

SESSION reserved variable

	{* display the php session variable "id" *}
	{ $templatelite[SESSION][id] }
	{ $templatelite.SESSION.id }
	

TEMPLATE reserved variable

	{* display the name of the template currently being processed. *}
	{ $templatelite[TEMPLATE] }
	{ $templatelite.TEMPLATE }
	

VERSION reserved variable

	{* display the version of Template Lite *}
	{ $templatelite[VERSION] }
	{ $templatelite.VERSION }
	


Home