![]() |
Flat-Frog - the faster compiling PHP template engine |
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.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
bbcode2html
will convert UBB-style tags into HTML. The following tags are converted:
[b][/b]
[u][/u]
[i][/i]
[email]email@address.com[/email]
or [email=email@address.com][/email]
[url]http://www.paullockaby.com/[/url]
or [url=http://www.paullockaby.com][/url]
[img]urltoimage.jpg[/img]
[code][/code]
[pre][/pre]
[list][/list]
[*]
(a list bullet)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
strtoupper
. It will capitalize the variable it modifies.
TEMPLATE ============================= <% "This is some text."|capitalize %> OUTPUT ============================= THIS IS SOME TEXT.
count
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.
PHP ============================= $tpl->assign("var", array("value1", "value2", "value3")); TEMPLATE ============================= <% $var|@count %> OUTPUT ============================= 3
date
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
.
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
PHP ============================= $tpl->assign("variable",""); $tpl->assign("value","here i am"); TEMPLATE ============================= <% $variable|default:"nothing" %> <% $value|default:"something" %> OUTPUT ============================= nothing here i am
lower
strtolower
. This will make the variable it modifies all lower-case.
TEMPLATE ============================= <% "This Is Some TEXT"|lower %> OUTPUT ============================= this is some text
nl2br
nl2br
. This will convert all new lines, i.e. \n
and \r
to <BR>.
TEMPLATE ============================= <% "This text has some spaces in it."|nl2br %> OUTPUT ============================= This text<BR>has some spaces in it.
number_format
number_format
. Using number_format
, you can change the number of decimals, the default decimal point, and the thousands separator.
TEMPLATE ============================= <% "1234.5645"|number_format %> <% "1234.5645"|number_format:2:'&':'' %> OUTPUT ============================= 1,234 1234&56
replace
str_replace
, except that the argument order is different. The first argument is the search string, the second is the replacement string.
TEMPLATE ============================= <% "I hate beans."|replace:"hate":"like" %> OUTPUT ============================= I like beans.
string_format
printf
. (Though technically it is a wrapper for vsprintf
, but printf
is more accurate in its function.)
TEMPLATE ============================= <% "Number %d is a %s."|string_format:"45":"pitcher" %> OUTPUT ============================= Number 45 is a pitcher.
strip
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
strip_tags
. Basically, it removes all HTML tags from the string it modifies. Well, technically, everything between < and >.
TEMPLATE ============================= <% "This is bold."|strip_tags %> OUTPUT ============================= This is a bold tag.
truncate
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
strtoupper
. This will capitalize the variable or phrase it modifies.
TEMPLATE ============================= <% "This is some text."|upper %> OUTPUT ============================= THIS IS SOME TEXT.
wordwrap
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.
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.