Flat-Frog - the faster compiling PHP template engine

Modifiers


Again, much like Smarty, Flat-Frog supports variable modifiers. At present, Flat-Frog 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);
	}
	
Flat-Frog 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.
	


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
	=============================
	<% $_TPL[NOW]|date:"n/j/Y g:ia" %>
	<% $_TPL[NOW]|date:"l, F j, Y" %>

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


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
	


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
	


nl2br

Description

This is a wrapper for nl2br. This will convert all new lines, i.e. \n and \r to <BR>.

Example

	TEMPLATE
	=============================
	<% "This text
	has some spaces in it."|nl2br %>

	OUTPUT
	=============================
	This text<BR>has some spaces in it.
	


number_format

Description

This is a wrapper for number_format. Using number_format, you can change the number of decimals, the default decimal point, and the thousands separator.

Arguments

Example

	TEMPLATE
	=============================
	<% "1234.5645"|number_format %>
	<% "1234.5645"|number_format:2:'&':'' %>

	OUTPUT
	=============================
	1,234
	1234&56
	


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.
	


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.
	


strip_tags

Description

This is a wrapper for strip_tags. Basically, it removes all HTML tags from the string it modifies. Well, technically, everything between < and >.

Example

	TEMPLATE
	=============================
	<% "This is bold."|strip_tags %>

	OUTPUT
	=============================
	This is a bold tag.
	


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.
	


wordwrap

Description

This is a wrapper for wordwrap. This will insert a \n at appropriate places, as specified by the arguments. The first argument is the length of a line, with a default of 80. The second argument is the string to insert at the wrapping point, with a default of \n. The last argument is a boolean indicating if you wish to break at words or at spaces only.

Arguments

Example

	TEMPLATE
	=============================
	<% "The quick brown fox jumped over the lazy dog."|wordwrap:20 %>
	<% "A very long woooooooooooord."|wordwrap:8:"\n":true %>

	OUTPUT
	=============================
	The quick brown fox\njumped over the \nlazy dog.
	A very\nlong\nwooooooo\nooooord.
	


Home