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.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
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.
cat
TEMPLATE ============================= { "This Is Some TEXT"|cat:" for you." } OUTPUT ============================= This Is Some Text for you.
count_characters
TEMPLATE ============================= { "This Is Some TEXT"|count_characters } { "This Is Some TEXT"|count_characters:true } OUTPUT ============================= 14 17
count_paragraphs
TEMPLATE ============================= { "Hi there everyone.\nThis is for you."|count_paragraphs } OUTPUT ============================= 2
count_sentences
TEMPLATE ============================= { "Hi there everyone. This is for you."|count_sentences } OUTPUT ============================= 2
count_words
TEMPLATE ============================= { "Hi there everyone"|count_words } OUTPUT ============================= 3
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 ============================= { $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
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
.
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
PHP ============================= $tpl->assign("variable",""); $tpl->assign("value","here i am"); TEMPLATE ============================= { $variable|default:"nothing" } { $value|default:"something" } OUTPUT ============================= nothing here i am
escape
TEMPLATE ============================= { "'This Is Some TEXT'"|escape } { "'This Is Some TEXT'"|escape:"url" } OUTPUT ============================= 'This Is Some TEXT' %27This+Is+Some+TEXT%27
indent
indent
. This will indent the text.
TEMPLATE ============================= { "This Is Some TEXT"|indent:10 } { "This Is Some TEXT"|indent:1:"\t" } OUTPUT ============================= this is some text this is some text
lower
strtolower
. This will make the variable it modifies all lower-case.
TEMPLATE ============================= { "This Is Some TEXT"|lower } OUTPUT ============================= this is some text
regex_replace
preg_replace
.
TEMPLATE ============================= { "Infertility unlikely to\nbe passed on, experts say."|regex_replace:" /[\r\t\n]/":" " } OUTPUT ============================= Infertility unlikely to be passed on, experts say.
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.
spacify
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
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.
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.
urlencode
TEMPLATE ============================= { "my stuff"|urlencode } OUTPUT ============================= my%20stuff