The std/system library in Tim Engine provides the standard functions, built-in operators (like +, -, *) and also comparison operators (like ==, !=, <, >) that are essential for performing various operations in your templates and scripts. This library is automatically imported into every template, so you can use its functions and operators without needing to import it explicitly.
The math utilities (abs, min, max, round, floor, ceil, sqrt) are also part of the system library — there is no separate std/math module to import.
🔗Operators
The system library registers the arithmetic, comparison, assignment and concatenation operators for int, float, bool and json values:
- Arithmetic:
+,-,*,/ - Assignment:
+=,-=,*=,/= - Comparison:
==,!=,<,<=,>,>= - Logical:
not(x) - Identity:
is(a, b),isnot(a, b)— strict type-and-value comparison - Concatenation:
&— works withstring,int,float,boolandjsonoperands (e.g."Hello " & name,$count & " items")
🔗Type conversion
🔗toInt
fn toInt(f: float): intConvert a float to an integer.
🔗parseInt
fn parseInt(s: string): intParse a string into an integer.
🔗toFloat
fn toFloat(i: int): float
fn toFloat(i: json): floatConvert an integer or JSON number to a float.
🔗toBool
fn toBool(x: int): bool
fn toBool(x: string): bool
fn toBool(x: json): boolConvert a value to a boolean.
🔗toString
fn toString(x: int): string
fn toString(x: float): string
fn toString(x: bool): string
fn toString(x: json): string
fn toString(x: object): stringConvert a value to its string representation. An object is converted to a JSON string.
🔗type
fn type(x: any): stringReturn the type name of a value as a string.
🔗jsonType
fn jsonType(x: json): stringReturn the type name of a JSON value.
🔗intVal / strVal
fn intVal(x: json): int
fn strVal(x: json): stringExtract the integer or string value from a JSON value.
🔗Length and index helpers
🔗len
fn len(x: string): int
fn len(x: json): int
fn len(x: array): intReturn the length of a string, JSON value or array.
🔗high
fn high(x: json): int
fn high(x: array): intReturn the highest valid index of a JSON value or array (i.e. len - 1).
🔗inc / dec
fn inc(i: int)
fn dec(i: int)Increment or decrement an integer in place.
🔗Math functions
🔗abs
fn abs(n: int): int
fn abs(n: float): floatReturn the absolute value of n.
🔗min / max
fn min(a: int, b: int): int
fn min(a: float, b: float): float
fn max(a: int, b: int): int
fn max(a: float, b: float): floatReturn the smaller / larger of the two values.
🔗round / floor / ceil
fn round(n: float): int
fn floor(n: float): int
fn ceil(n: float): intRound n to the nearest integer, round down, or round up.
🔗sqrt
fn sqrt(n: float): floatReturn the square root of n.
🔗OS operations
The std/system library provides a minimal set of functions for performing various OS operations, such as reading and writing files, sleeping for a specified duration.
🔗readFile
fn readFile(path: string): stringRead a file and return its content as a string.
🔗writeFile
fn writeFile(path: string, content: string)Write a file with the specified content.
🔗sleep
fn sleep(ms: int)Sleep for a specified duration (in milliseconds).
🔗JSON parsing
The std/system library also includes functions for parsing JSON data, which can be useful for working with APIs or configuration files.
🔗parseJSON
fn parseJSON(content: string): jsonParse a JSON string and return it as a JSON value.
🔗loadJSON
fn loadJSON(path: string): jsonLoad JSON data from a file and return it as a JSON value.
🔗remoteJSON
fn remoteJSON(url: string): jsonLoad JSON data from a remote URL and return it as a JSON value. This function performs an HTTP GET request to the specified URL and parses the response as JSON.
🔗fetch
fn fetch(url: string): json
fn fetch(url: string, options: json): jsonPerform an HTTP request and return the response as JSON. The options object can carry request settings.
🔗hasKey
fn hasKey(obj: json, key: string): boolCheck if a JSON object contains the given key.
🔗Random
🔗shuffle
fn shuffle(arr: array): arrayReturn a new array with the elements in random order.
🔗Output
🔗echo
fn echo(x: string | int | float | bool | json | nil | object | array | pointer)Print a value to the standard output.
🔗assert
fn assert(condition: bool)Assert that a condition is true; raises an error if it is not.
🔗HTML escaping
🔗escape
fn escape(s: string): stringEscape HTML special characters in s (&, <, >, ", ').
🔗unescape
fn unescape(s: string): stringDecode HTML entities back into their original characters.