Skip to main content Link Search Menu Expand Document (external link)

Expression Functions Reference

Built-in functions for data manipulation and formatting in Scryber binding expressions.


On this page

Overview

Expression functions transform and manipulate data within binding expressions. Over 90 built-in functions are available across multiple categories.

Usage:


{{functionName(param1, param2, ...)}}


Conversion Functions

Convert values between different types.

Function Description Example
int Convert to integer {{int(model.value)}}
long Convert to long integer {{long(model.bigNumber)}}
double Convert to double precision float {{double(model.value)}}
decimal Convert to decimal {{decimal(model.price)}}
bool Convert to boolean {{bool(model.flag)}}
date Convert to DateTime {{date(model.dateString)}}
typeof Get type name {{typeof(model.value)}}

String Functions

Manipulate and format text.

Function Description Example
format / string Format values as strings {{format(model.price, 'C2')}}
concat Concatenate strings {{concat(model.first, ' ', model.last)}}
join Join array with separator {{join(model.items, ', ')}}
substring Extract substring {{substring(model.text, 0, 10)}}
replace Replace text {{replace(model.text, 'old', 'new')}}
toLower Convert to lowercase {{toLower(model.text)}}
toUpper Convert to UPPERCASE {{toUpper(model.code)}}
trim Remove leading/trailing whitespace {{trim(model.text)}}
trimEnd Remove trailing whitespace {{trimEnd(model.text)}}
length Get string length {{length(model.text)}}
contains Check if contains substring {{contains(model.text, 'search')}}
startsWith Check if starts with substring {{startsWith(model.text, 'prefix')}}
endsWith Check if ends with substring {{endsWith(model.text, 'suffix')}}
indexOf Find substring position {{indexOf(model.text, 'search')}}
padLeft Pad left with characters {{padLeft(model.num, 5, '0')}}
padRight Pad right with characters {{padRight(model.text, 10, ' ')}}
split Split string into array {{split(model.text, ',')}}
regexIsMatch Test regex pattern {{regexIsMatch(model.email, '^.+@.+$')}}
regexMatches Find all regex matches {{regexMatches(model.text, '\\d+')}}
regexSwap Replace using regex {{regexSwap(model.text, '\\d+', 'X')}}

Mathematical Functions

Perform calculations and mathematical operations.

Function Description Example
abs Absolute value {{abs(model.value)}}
ceiling Round up to integer {{ceiling(model.value)}}
floor Round down to integer {{floor(model.value)}}
round Round to nearest {{round(model.value, 2)}}
truncate Truncate decimal {{truncate(model.value)}}
sqrt Square root {{sqrt(model.value)}}
pow Raise to power {{pow(model.base, model.exp)}}
exp Exponential (e^x) {{exp(model.value)}}
log Natural logarithm {{log(model.value)}}
log10 Base-10 logarithm {{log10(model.value)}}
sign Sign of number (-1, 0, 1) {{sign(model.value)}}
sin Sine {{sin(model.radians)}}
cos Cosine {{cos(model.radians)}}
tan Tangent {{tan(model.radians)}}
asin Arcsine {{asin(model.value)}}
acos Arccosine {{acos(model.value)}}
atan Arctangent {{atan(model.value)}}
degrees Convert radians to degrees {{degrees(model.radians)}}
radians Convert degrees to radians {{radians(model.degrees)}}
pi Pi constant (3.14159…) {{pi()}}
e Euler’s number (2.71828…) {{e()}}
random Random number {{random()}}

Date & Time Functions

Work with dates and timestamps.

Function Description Example
addDays Add days to date {{addDays(model.date, 7)}}
addMonths Add months to date {{addMonths(model.date, 1)}}
addYears Add years to date {{addYears(model.date, 1)}}
addHours Add hours to date {{addHours(model.date, 2)}}
addMinutes Add minutes to date {{addMinutes(model.date, 30)}}
addSeconds Add seconds to date {{addSeconds(model.date, 45)}}
addMilliseconds Add milliseconds to date {{addMilliseconds(model.date, 500)}}
daysBetween Days between two dates {{daysBetween(model.start, model.end)}}
hoursBetween Hours between two dates {{hoursBetween(model.start, model.end)}}
minutesBetween Minutes between two dates {{minutesBetween(model.start, model.end)}}
secondsBetween Seconds between two dates {{secondsBetween(model.start, model.end)}}
yearOf Extract year {{yearOf(model.date)}}
monthOfYear Extract month (1-12) {{monthOfYear(model.date)}}
dayOfMonth Extract day (1-31) {{dayOfMonth(model.date)}}
dayOfWeek Extract day of week (0-6) {{dayOfWeek(model.date)}}
dayOfYear Extract day of year (1-366) {{dayOfYear(model.date)}}
hourOf Extract hour (0-23) {{hourOf(model.date)}}
minuteOf Extract minute (0-59) {{minuteOf(model.date)}}
secondOf Extract second (0-59) {{secondOf(model.date)}}
millisecondOf Extract millisecond (0-999) {{millisecondOf(model.date)}}

Logical Functions

Control flow and conditional logic within expressions.

Function Description Example
if Ternary conditional {{if(model.active, 'Yes', 'No')}}
ifError Fallback on error {{ifError(model.value, 'default')}}
in Check if value in list {{in(model.status, 'active', 'pending')}}

Collection Functions

Aggregate and manipulate collections.

Function Description Example
count Count items in collection {{count(model.items)}}
countOf Count with condition {{countOf(model.items, 'isActive')}}
sum Sum numeric values {{sum(model.numbers)}}
sumOf Sum property values {{sumOf(model.items, 'price')}}
min Minimum value {{min(model.numbers)}}
minOf Minimum property value {{minOf(model.items, 'price')}}
max Maximum value {{max(model.numbers)}}
maxOf Maximum property value {{maxOf(model.items, 'price')}}
collect Extract property values {{collect(model.items, 'name')}}
each Iterate with function {{each(model.items, 'transform')}}
eachOf Iterate property with function {{eachOf(model.items, 'prop', 'fn')}}
firstWhere Find first matching item {{firstWhere(model.items, 'isActive')}}
selectWhere Filter collection {{selectWhere(model.items, 'isActive')}}
sortBy Sort by property {{sortBy(model.items, 'name')}}
reverse Reverse order {{reverse(model.items)}}

Statistical Functions

Calculate statistics on collections.

Function Description Example
average Average of values {{average(model.numbers)}}
averageOf Average of property {{averageOf(model.items, 'price')}}
mean Mean value {{mean(model.numbers)}}
median Median value {{median(model.numbers)}}
mode Mode (most frequent) {{mode(model.numbers)}}

CSS Functions

Dynamic CSS calculations.

Function Description Example
calc CSS calc expression {{calc('100% - 20pt')}}
var CSS variable reference {{var('primary-color')}}

Common Patterns

Formatting Currency


<p>Total: {{format(model.total, 'C2')}}</p>
<!-- Output: Total: $1,234.56 -->

Date Calculations


<p>Due: {{format(addDays(model.orderDate, 30), 'MMMM dd, yyyy')}}</p>
<!-- Output: Due: April 14, 2024 -->

Conditional Values


<span class="{{if(model.score >= 70, 'pass', 'fail')}}">
  Score: {{model.score}}
</span>
<!-- Output: <span class="pass">Score: 85</span> -->

Collection Aggregation


<p>Total Items: {{count(model.items)}}</p>
<p>Total Price: {{format(sumOf(model.items, 'price'), 'C2')}}</p>
<p>Average: {{format(averageOf(model.items, 'price'), 'C2')}}</p>

String Manipulation


<h2>{{toUpper(substring(model.title, 0, 1))}}{{substring(model.title, 1)}}</h2>
<!-- Output: Capitalize first letter -->


See Also