[CS1] Template Functions

There are a number of functions you can use inside the  template language. Functions must appear inside of braces.

{ ucfirst('jim') }

The following is a complete list of available functions.

Function Description Example Output Video
abs(number) Returns the absolute value of a number. abs(2)
abs(-2)
2
2
ceil(decimal) Returns the largest integer from a decimal number. ceil(4.2) 5
cos(number) Returns the cosine of the number. cos(0) 1
floor(decimal) Returns the smallest integer from a decimal number. floor(4.2) 4
format(pattern, var1) Formats a string according to a pattern.

%% - a percent sign
%s - a string of text
%d - an integer
%2d - an integer that uses 2 characters and pads with spaces
%02d - an integer that uses 2 characters and pads with zeros
%.1f - a floating point number with 1 digit after the decimal
%.2f - a floating point number with 2 digits after the decimal
format('Hello %s', $name)
format('%s %s', $color, $noun)    
format('%.2f', 7 / 3)
format('%.1f', 7 / 3)

format('%d', 7 / 3)
format('%2d', 7 / 3)
format('%02d', 7 / 3)


Hello Dave
red hand

2.33
2.3

2
 2
02
get(variable) Returns a variable set earlier using the set() function. get('foo') bar Video
hexstr(integer) Returns a hexadecimal string from an integer or hex value. hexstr(13)
hexstr(0xF - c)
d
c
hexstr2(integer) Same as hexstr(), but pre-pads with a zero on single digit. This is perfect for creating hex triplets in calculated colors, since RGB is 3 sets of hex values. hexstr2(255)
hexstr2(1)
hexstr2(0xFF - 10)
ff
01
f5
lc(string) Lower cases a string. lc('aBcDeF') abcdef
length(string) Returns the number of characters in a string. length('crafter') 7
max(number, max) Returns a number equal to or lower than the max. max(5,3)
max(1,3)
3
1
min(number, min) Returns a number equal to or higher than the min. min(5,3)
min(1,3)
5
3
plural(integer, format) Allows you to create pluralized text in an easy manner based upon what the value of the integer is.

There are some formatting symbols you should know:

%d - the quantity
() - optional, if plural
(||) - optional, inverted 
{|} - options

plural(0,'%d piece(s)')
plural(1,'%d piece(s)')
plural(2,'%d piece(s)')

plural(0,'action(s) need{|s|} to be taken')
plural(1,'action(s) need{|s|} to be taken')
plural(2,'action(s) need{|s|} to be taken')

plural(0,'{No|%d} cand(y|ies) (is|are) eaten')
plural(1,'{No|%d} cand(y|ies) (is|are) eaten')
plural(2,'{No|%d} cand(y|ies) (is|are) eaten')

plural(0,'{No|One|Two|Three|%d} item(s)')
plural(1,'{No|One|Two|Three|%d} item(s)')
plural(2,'{No|One|Two|Three|%d} item(s)')
plural(4,'{No|One|Two|Three|%d} item(s)')
0 pieces
1 piece
2 pieces

actions need to be taken
action needs to be taken
actions need to be taken

No candies are eaten
1 candy is eaten
2 candies are eaten

No items

One item

Two items

4 items
randint(a,b) Returns a random integer between two integers (inclusive). randint(1,10)
randint(1,10)
randint(1,10)
3
1
9
range(number, min, max) Returns a number between min and max (inclusive). range(1,3,5)
range(3,1,5)
range(5,1,3)
3
3
3
replace(find, replace, string) Replaces 1 string with another inside a third string. replace('x', 'X', 'Text')
replace('', ' - ', 'Text')
replace('water', '*w*', 'Requires 3 water.')
TeXt
T - e - x - t
Requires 3 *w*.
round(decimal) Returns the smallest integer from a decimal number if the remainder is less than 5 or the highest when it's 5 or above. round(4.2) 4
set(variable, value) Set a variable for retrieval later. set('foo','bar')
setget(variable, value) Set a variable for retrieval later, but also output it now. setget('foo','bar') bar Video
sin(number) Returns the sine of the number. sin(0) 0
sqrt(number) Returns the square root of the number. sqrt(4) 2
uc(string) Upper cases a string. uc('aBcDeF') ABCDEF
ucfirst(string) Upper cases the first letter in a string. ucfirst('aBcDeF') ABcDeF

There are also a number of functions provided just for dealing with tables.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us