Skip to content

Latest commit

 

History

History
526 lines (408 loc) · 12 KB

builtin-functions.md

File metadata and controls

526 lines (408 loc) · 12 KB

Sempare Template Engine

Copyright (c) 2019-2024 Sempare Limited

Builtin functions

char functions

chr(val)

Convert a numeric value to the character counterpart.

<% chr(10) %>

ord(char)

Convert a character to the numeric counterpart.

<% ord('a') %>

date functions

dtnow()

Returns the SysUtils now() date time. <% dtnow() %>

fmtdt(format[, dt])

Formats a date time using FormatDateTime

<% fmtdt('yyyy-mm-dd', dtnow()) %>

For formatting options, see http://docwiki.embarcadero.com/Libraries/Rio/en/System.SysUtils.FormatDateTime#Description

dataset functions

isdataset(object)

return true if the object is a TDataSet

<% isdataset(ds) %>

recordcount(dataset)

return the length of a dataset

<% RecordCount(ds) %>

encoding/decoding functions

base64decode(string)

returns the base64 decoded value

<% base64decode('aGVsbG8gd29ybGQ=') %>

base64encode(string)

returns the base64 encoded value

<% base64encode('hello world') %>

htmlescape(string)

returns the html escaped value

<% htmlescape('<a href="https://www.google.com">google</a>') %>

htmlunescape(string)

returns the html escaped value

<% htmlunescape('&amp;lt;a href=&amp;quot;https://www.google.com&amp;quot;&amp;gt;google&amp;lt;/a&amp;gt;') %>

hashing functions

md5(string)

returns the md5 hash of the string

<% md5('hello world') %>

sha1(string)

returns the sha1 hash of the string

<% sha1('hello world') %>

sha256(string)

returns the sha256 hash of the string

<% sha1('hello world') %>

misc functions

isempty(value)

Returns true if the collection is an object, it is not nil, and it contains values. If you have custom containers, the rtti unit provides RegisterEmptyObjectCheck() to enhance this method.

<% isempty(list) %>
<% isempty(dict) %>
<% isempty(queue) %>
<% isempty(stack) %>
<% isempty(dataset) %>

containskey(map, key)

Returns true if the map contains the key. The key must be a string.

<% containskey({}, "key") %> // false
<% containskey({"key":"value"}, "key") %> // true

default(value, default)

Returns value if value is not empty, otherwise default

<% name := '' %>
<% default(name, 'joe bloggs') %> 

domid(value[, context])

Returns a string composed of a type and an id. Useful for creating unique DOM ids in HTML.

This works with records and classes, where the function inspects the structure for an 'id' field.

type
  TMyRec = record
    id : string;
  end;
  
...
  var LRec : TMyRec;
  LRec.id := '123';
  var LResult := Template.Parse('<% domid(_) %>', LRec); // TMyRec_123
  
  LResult := Template.Parse('<% domid(_, ''ctx'') %>', LRec); // ctx_TMyRec_123
  

manage(object)

Manages an object so that you don't have to explicitly free objects.

<% a := manage(aFuncCreatingAnObject()) %>

Also see unmanage(object)

sort(array)

Sorts basic arrays of integer, double, extended and string or something enumerable of these types.

<% values := sort(split('g,f,d,s,d,a', ',')) %>

templateexists(string)

returns true if the template exists

<% templateexists('template') %>

unmanage(object)

Unmanages an object so that you don't have to explicitly free objects.

<% a := manage(aFuncCreatingAnObject()) %>
<% unmanage(a) %>
<% a.free() %>

numeric functions

abs(value)

return the absolute value of a value

<% abs(-123.45) %>

min(adouble, bdouble)

return the minimum of two values

<% min(1,2) %>

max(adouble, bdouble)

return the maximum of two values

<% max(1,2) %>

string functions

crnl(len)

return a string with len #13#10

<% crnl(2) %>

endswith(string, substr[, ignoreCase=true])

Check if a string ends with another.

<% endswith('heLlo', 'lo') %>

fmt(format[, args...])

Allows a string to be formatted using SysUtils format().

<% fmt('%s %s %d', 'hello','world', 123) %>

For formatting options, see: http://docwiki.embarcadero.com/Libraries/Rio/en/System.SysUtils.Format#Format_Strings

len(string)

Return the length of a string or an array.

<% len('hello world') %>

lowercase(string)

Lowercase a string.

<% lowercase('heLlo') %>

match(text, regex)

Matches text in a regular expression.

<% match('aaaaaaaaaaaaaa', 'a+') %>

For more information, see http://docwiki.embarcadero.com/Libraries/Rio/en/System.RegularExpressions.TRegEx.Matches

nl(len)

return a string with len #10

<% nl(2) %>

padleft(str, len[, padchar=' '])

Pad string with padchar from the left till the string is len long.

<% padleft('123', 6) %>

padright(str, len[, padchar=' '])

Pad string with padchar from the right till the string is len long.

<% padright('123', 6) %>

pos(substr, string[, offset])

Return an offset of substr within string from offset. if offset is less than 0, it will be the offset from the end of the string.

pos('lo', 'hello')

replace(search, replacement, str)

Replace some text with another.

<% replace('a', 'hello ', 'aaa') %>

rev(string)

Reverse a string.

<% rev('abc') %>

spaces(len)

return len spaces

<% spaces(2) %>

split(string, sep)

Split the string using a seperator returning an array of string.

<% split('hello world', ' ')[0] %>

startswith(string, substr[, ignoreCase=true])

Check if a string starts with another.

<% startswith('heLlo', 'he') %>

substr(string, start[, length])

Return a substring starting at 'start'. If 'start' is less than 0, it will be the offset from the end of the string.

<% substr(' hello ', 2, 5) %>

substring(string, start[, end])

Return a substring starting at 'start'. If 'start' or 'end' is less than 0, it will be the offset from the end of the string.

<% substring(' hello ', 2, 6) %>

tabs(len)

Return len tabs

<% tabs(2) %>

trim(string) / trimleft(string) / trimright(string)

Remove whitespace from a string.

<% trim(' hello ') %>

ucfirst(string)

Uppercase the first letter of a string with the rest being lowercase.

<% ucfirst('hello') %>

uppercase(string)

Uppercase a string.

<% uppercase('heLlo') %>

type conversion functions

bool(any)

bool() casts to a boolean.

<% bool(true) %>    // true
<% bool('true') %>  // false
<% bool(false) %>   // true
<% bool('false') %> // false
<% bool(1) %>       // false
<% bool(0) %>       // false
<% bool(10) %>      // false
<% bool(-10) %>     // false
<% bool(1.23) %>    // false

int(any)

int() casts a number to an integer.

<% int(123.45) %>
<% int('123.45') %>

num(any)

num() casts the variable to a number.

<% num(true) %>    // 1
<% num(10) %>      // 10
<% num('123') %>   // 123<br>
<% num('1e6') %>   //1000000

str(any)

str() casts a variable to a string. isstr() checks if a variable is a string.

<% str(123) + ' = one two three' %>

type checking functions

isbool(any)

isbool() checks if a variable is a boolean.

<% isbool(true) %>    // true
<% isbool('true') %>  // false
<% isbool(false) %>   // true
<% isbool('false') %> // false
<% isbool(1) %>       // false
<% isbool(0) %>       // false
<% isbool(1.23) %>    // false

isint(any)

isint() checks if a variable is an integer.

<% isint(123) %>      // true
<% isint(123.45) %>   // false
<% isint('123.45') %> // false

isnil(any) / isnull(any)

isnull()/isnil() checks if an object is null or not.

<% isnil(dataset) %>    // 

isnum(any)

isnum() checks if a variable is an number.

<% isnum(123) %>      // true
<% isnum(123.45) %>   // true
<% isnum('123.45') %> // false

ismap(any)

ismap() checks if a variable is a map.

<% ismap(123) %>      // false
<% ismap({}) %>   // true
<% ismap({"a":123}) %>   // true

isobject(any)

isobject() checks if a variable is an object.

<% isobject(obj) %>

isrecord(any)

isrecord() checks if a variable is a record.

<% isrecord(obj) %>

isstr(any)

isstr() checks if a variable is an string.

<% isstr(123) %>      // false
<% isstr(bool) %>     // false
<% isstr('123.45') %> // true

typeof(obj)

Return the class name of an object.

<% typeof(true) %>     // System.Boolean
<% typeof(123) %>      // System.Extended
<% typeof(123.45) %>   // System.Extended
<% typeof('test') %>   // System.string