Template Lite - the faster compiling PHP template engine

Modifiers


Again, much like Smarty, Template Lite supports variable modifiers. At present, Template Lite comes with a few modifiers, namely those listed below. Additionally, modifiers are extremely easy to create. Here are some examples of using them in various situations and how to create one.

	{* uppercase the title *}
	<h2>{ $title|upper }</h2>

	{* truncate the topic to 40 characters and use '...' at the end *}
	Topic: { $topic|truncate:40:"..." }

	{* uppercase and truncate the topic *}
	Topic: { $topic|truncate:40:"..."|upper }
	
Modifiers can be referenced in two ways. You can create a random function in your program somewhere, following the function format of $string, $arg1, $arg2, etc. and then dynamically register it (which is faster), or you can put your modifier in a file and put it in the plugins directory in the format modifier.modifiername.php. The advantage of putting it in the plugins directory is that it only gets included if it is used and once it is included, subsequent calls on the same page are faster. NOTE: Modifiers are applied left to right. That means that the first modifier in the list is applied first, the second is applied second, etc. In the above example, $topic would first be truncated, then uppercased.

Here is an example of how to create a modifier:
	function tpl_modifier_upper($string) {
		return strtoupper($string);
	}
	
Template Lite comes with a whole bunch of modifiers in the plugin directory. Here is a brief description and usage of each one.


bbcode2html

Description

bbcode2html will convert UBB-style tags into HTML. The following tags are converted:

Example

	TEMPLATE
	=============================
	{ "This will be [b]bold[/b]. My website is 
	[url=http://www.paullockaby.com/]paullockaby.com[/url]."|bbcode2html }

	OUTPUT
	=============================
	This will be <B>bold</B>. My website is 
	<a href="http://www.paullockaby.com">paullockaby.com</a>.
	


capitalize

Description

This is a wrapper for strtoupper. It will capitalize the variable it modifies.

Example

	TEMPLATE
	=============================
	{ "This is some text."|capitalize }

	OUTPUT
	=============================
	THIS IS SOME TEXT.
	


cat

Description

This concatenates two items into one.

Arguments

Example

	TEMPLATE
	=============================
	{ "This Is Some TEXT"|cat:" for you." }

	OUTPUT
	=============================
	This Is Some Text for you.
	


count_characters

Description

This is used to count the number of characters in a variable.

Arguments

Example

	TEMPLATE
	=============================
	{ "This Is Some TEXT"|count_characters }
	{ "This Is Some TEXT"|count_characters:true }

	OUTPUT
	=============================
	14
	17
	


count_paragraphs

Description

This is used to count the number of paragraphs in a variable.

Example

	TEMPLATE
	=============================
	{ "Hi there everyone.\nThis is for you."|count_paragraphs }

	OUTPUT
	=============================
	2
	


count_sentences

Description

This is used to count the number of sentences in a variable.

Example

	TEMPLATE
	=============================
	{ "Hi there everyone. This is for you."|count_sentences }

	OUTPUT
	=============================
	2
	


count_words

Description

This is used to count the number of words in a variable.

Example

	TEMPLATE
	=============================
	{ "Hi there everyone"|count_words }

	OUTPUT
	=============================
	3
	


count

Description

This is a wrapper for count. It will return the number of elements in the variable it modifies if that variable is an array. In order to effectively use this modifier, you should prepend it with an '@' in order to apply it to the entire array, instead of each individual element of the array.

Example

	PHP
	=============================
	$tpl->assign("var", array("value1", "value2", "value3"));

	TEMPLATE
	=============================
	{ $var|@count }

	OUTPUT
	=============================
	3
	


date

Description

This is a wrapper for date. This will format a given Unix timestamp to format. If the date given empty, this will return the current date and time, or use the given date and time in default_date.

Arguments

Example

	TEMPLATE
	=============================
	{ $templatelite[NOW]|date:"n/j/Y g:ia" }
	{ $templatelite[NOW]|date:"l, F j, Y" }

	OUTPUT
	=============================
	7/27/2003 5:54pm
	Sunday, June 27, 2003
	


date

Description

This is a wrapper for date_format. This will format a given Unix timestamp to format. If the date given empty, this will return the current date and time, or use the given date and time in default_date.

Arguments

Example

	TEMPLATE
	=============================
	{ $templatelite[NOW]|date_format:" %I:%M %p" }
	{ $templatelite[NOW]|date_format: "%A, %B %e, %Y" }

	OUTPUT
	=============================
	02 :33 pm
	Monday , February 5, 2001
	


default

Description

If a variable does not contain a value, this will put a default value in its place. Note that zero (0) is not considered as empty and will not trigger a default value.

Arguments

Example

	PHP
	=============================
	$tpl->assign("variable","");
	$tpl->assign("value","here i am");

	TEMPLATE
	=============================
	{ $variable|default:"nothing" }
	{ $value|default:"something" }

	OUTPUT
	=============================
	nothing
	here i am
	


escape

Description

This is used to html escape, url escape, escape single quotes on a variable not already escaped, hex escape, hexentity or javascript escape. By default, the variable is html escaped.

Arguments

Example

	TEMPLATE
	=============================
	{ "'This Is Some TEXT'"|escape }
	{ "'This Is Some TEXT'"|escape:"url" }

	OUTPUT
	=============================
	'This Is Some TEXT'
	%27This+Is+Some+TEXT%27
	


indent

Description

This is a wrapper for indent. This will indent the text.

Arguments

Example

	TEMPLATE
	=============================
	{ "This Is Some TEXT"|indent:10 }
	{ "This Is Some TEXT"|indent:1:"\t" }

	OUTPUT
	=============================
	          this is some text
	    this is some text
	


lower

Description

This is a wrapper for strtolower. This will make the variable it modifies all lower-case.

Example

	TEMPLATE
	=============================
	{ "This Is Some TEXT"|lower }

	OUTPUT
	=============================
	this is some text
	


regex_replace

Description

This is a wrapper for preg_replace.

Arguments

Example

	TEMPLATE
	=============================
	{ "Infertility unlikely to\nbe passed on, experts say."|regex_replace:" /[\r\t\n]/":" " }

	OUTPUT
	=============================
	Infertility unlikely to be passed on, experts say.
	


replace

Description

This is a wrapper for str_replace, except that the argument order is different. The first argument is the search string, the second is the replacement string.

Arguments

Example

	TEMPLATE
	=============================
	{ "I hate beans."|replace:"hate":"like" }

	OUTPUT
	=============================
	I like beans.
	


spacify

Description

Spacify can insert a space between every character of a variable. You can optionally pass a different character (or string) to insert.

Arguments

Example

	TEMPLATE
	=============================
	{ $articleTitle|spacify}
	{ $articleTitle|spacify:"^^"}

	OUTPUT
	=============================
	S o m e t h i n g .
	S^^o^^m^^e^^t^^h^^i^^n^^g^^.
	


string_format

Description

This is a wrapper for printf. (Though technically it is a wrapper for vsprintf, but printf is more accurate in its function.)

Arguments

This function has an unlimited number of arguments. Each argument is another replacement to be made on the string.

Example

	TEMPLATE
	=============================
	{ "Number %d is a %s."|string_format:"45":"pitcher" }

	OUTPUT
	=============================
	Number 45 is a pitcher.
	


strip

Description

This modifier will remove all repeated spaces, new lines, and tabs with a single instance.

Arguments

Example

	TEMPLATE
	=============================
	{ "Grandmother of\neight makes\t    hole in one." }
	{ "Grandmother of\neight makes\t    hole in one."|strip }
	{ "Grandmother of\neight makes\t    hole in one."|strip:" " }

	OUTPUT
	=============================
	Grandmother of
	eight makes        hole in one.
	Grandmother of eight makes hole in one.
	Grandmother of eight makes hole in one.
	


truncate

Description

This modifier will trim a variable to a specific length. The first argument is the cut off length, with a default of 80. The second argument is the extender (see example). The last argument is a boolean indicating if you wish to break at words or at spaces only.

Arguments

Example

	TEMPLATE
	=============================
	<% "Might we make this sentence a little shorter, not a long run-on?"|truncate }
	<% "Might we make this sentence a little shorter, not a long run-on?"|truncate:50:"..." }
	<% "Might we make this sentence a little shorter, not a long run-on?"|truncate:50:"...":true }

	OUTPUT
	=============================
	Might we make this sentence a little shorter, not a long run-on?
	Might we make this sentence a little shorter,...
	Might we make this sentence a little shorter, n...
	


upper

Description

This is a wrapper for strtoupper. This will capitalize the variable or phrase it modifies.

Example

	TEMPLATE
	=============================
	{ "This is some text."|upper }

	OUTPUT
	=============================
	THIS IS SOME TEXT.
	


urlencode

Description

This is used to convert a text into URL encoded text.

Example

	TEMPLATE
	=============================
	{ "my stuff"|urlencode }

	OUTPUT
	=============================
	my%20stuff
	


Home