Skip to content

template

Chris Lowth edited this page Sep 21, 2022 · 3 revisions

"template" function details.

For a description of the accepted template file format, see https://golang.org/pkg/text/template/. A number of functions have been added to the list of those available in the standard package. These are documented below.

For example, with the following text in the file t1.tmpl ..

Number = {{ add .x.num 20 }}

The following script will output the text "Number = 1254"

var values = { "num": 1234 };
function add(n1, n2) { return n1 + n2; }
var funcs = { "add": add };
var a = template("t1.tmpl", values, funcs);

Additional template functions provided (beyond those documented for the standard GoLang template module)..

Name Description
add {n}.. Returns the sum of all supplied arguments
call {cmd} {args}.. Returns the standard output resulting from calling the TBUtil command (or throws an error). See the call function, above.
date {fmt} {d} Returns the specified time/date in d formatted using d.format(fmt). If d is null, the current time/date is used. If fmt is null then "Monday, 2 Jan 2006" is used.
equals {a} {b} Type-agnostic version of the standard eq template function.
eval {str} Equivalent to eval(str) in JavaScript.
getenv {name} Equivalent to getenv(name) in TBScript.
hasPrefix {str} {substr} Equivalent to str.hasPrefix(substr) in TBScript.
hasSuffix {str} {substr} Equivalent to str.hasSuffix(substr) in TBScript.
ifelse {cond} {then} {else} If cond is true, then return then else return else.
ifNull {val} {default} Return default if val is null, otherwise return val.
include {fileName} [{obj}] Include the specified template file, optionally passing obj as the context.
ifNull {val} {repl} Returns val unless it is null, in which case repl is returned.
isNull {val} Returns true if val is null (or undefined).
json {obj} Equivalent to JSON.stringify(obj, "", " ") in JavaScript.
join {array} { str} Equivalent to array.join(str) in TBScript.
keys {obj} Eqivalent to _.keys(obj) in TBScript.
length {a} Eqivalent to a.length in JavaScript.
list {args}.. Return a list containing all the specified arguments.
lower {str} Returns the str converted to all lower case .
mult {n}.. Returns the product of all supplied arguments.
neg {n} Return -n.
null Returns null value.
numSort {list} Sort a list of numbers and return the sorted list.
parseJson {str} Eqivalent to JSON.parse(str) in JavaScript.
readFile {filename} Returns the content as the specified file as a string.
replace {str} {a} {b} Equivalent to str.replace(a, b) in JavaScript.
replaceAll {str} {re} {with} Replaces all matches of the regexp re in str with with.
shell {command} Runs the specified shell command and returns the displayed standard output text.
split {str} {delim} Eqivalent to str.split(delim) in TBScript.
sprintf {format} {args..} Equivalent to sprintf(format, args...) in TBSript.
strcat {str}.. Concatinate all the supplied arguments into a single string.
toFloat {value} Converts the integer, float or string value to a float.
toInt {value} Converts the integer, float or string value to an integer.
trim {str} Returns str with all leading and trailing white space removed.
trimPrefix {str} {prefix} Eqivalent to str.trimPrefix(prefix) in TBScript.
trimSuffix {str} {suffix} Eqivalent to str.trimSuffix(suffix) in TBScript.
upper {str} Returns the str converted to all upper case .
wrap {str} {len} Eqivalent to str.wordWrap(len) in TBScript.
yaml {obj} Return a string containing a YAML foratted representation of obj.

See also: the {string}.template(obj, funcMap) method for a way to use the same logic on a string instead of a file.

The template command can read the encrypted equivalent of the file if either the TURBO_PIN or TURBO_KEY environment variable is set. In this case, if a copy of the file with a ".cr" extension exists, that will be decrypted and loaded in place of the original.

Clone this wiki locally