Flat-Frog - 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 Flat-Frog. 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 in a similar way to PHP. You have the name of the variable followed by brackets containing the key of the array value that you want to view.
	PHP
	=============================
	$foo = array("apples", "oranges", "bananas");
	$tpl->assign("foo", $foo);

	TEMPLATE
	=============================
	<% $foo %>
	<% $foo[0] %>

	OUTPUT
	=============================
	Array
	apples
	

Associative Arrays

As was the case with basic variables, arrays are referenced in templates in a similar way to PHP. You have the name of the variable followed by brackets 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] %>

	OUTPUT
	=============================
	apples
	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 Flat-Frog 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 (not $foo.bar)
	<% "test `$foo.bar` test" %>	<- sees $foo
	<% 'test $foo test' %>		<- will interpret literally
	<% "test \$foo test" %>		<- will escape the variable and return $foo literally
	


Variable concatenation

The are two ways to concatenate variables using Flat-Frog. 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.


$_TPL reserved variable

Using the $_TPL variable, you can refer to special environment variables from inside the template.

	<%* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *%>
	<% $_TPL[GET][PAGE] %>

	<%* display the variable "page" from a form (POST) *%>
	<% $_TPL[POST][PAGE] %>

	<%* display the value of the cookie "username" *%>
	<% $_TPL[COOKIE][username] %>

	<%* display the server variable "SERVER_NAME" *%>
	<% $_TPL[SERVER][SERVER_NAME] %>

	<%* display the system environment variable "PATH" *%>
	<% $_TPL[ENV][PATH] %>

	<%* display the php session variable "id" *%>
	<% $_TPL[SESSION][id] %>

	<%* display the current time in unix epoch form *%>
	<% $_TPL[NOW] %>
	


Home