As MTPS is language-agnostic, things like arythmetic operations, logical operations, retreaving properties of objects, calling methods etc. are all done using the same syntax, called MPTS-Expression.
Expression can be used for example in element attributes, or between `` to produce text.
Literals
Literals are used to represent fixed values.
String
Strings are enclosed in either single quotes '
or double quotes "
.
Hello, World!
Hello, World!
Unicode characters can be represented using XML’s style.
ab <!-- "ab" -->
Number
Numbers are written without quotes.
42
3.14
Boolean
Boolean values are represented by the keywords true
and false
.
true
false
Null
The null value is represented by the keyword null
.
Variables
Expression can access variables that were provided when executing the template, or scoped variables created by
<:variable/>
, <:foreach/>
, or <:loop/>
.
Property access
You can access properties of objects or elements of arrays using the .
operator.
Function/Method calls
You can call functions or methods using the ()
operator.
MPTS has no built-in functions, so all functions must be provided when executing the template. Function is type of variable, so in JS provide jsut a function, in PHP provide a callable, in C# provide a delegate etc.
If you write jsut name of a function, without ()
, it will return the function itself, not the result of calling it.
<!-- returns the function itself -->
<!-- calls the function and returns the result -->
Parameters can be any expression, including other function calls.
Operators
Arithmetic operators
Arithmetic operators are used to perform mathematical operations on numbers.
Addition
The addition operator +
adds two numbers together.
2 <!-- 4 -->
Unlike in many programming languages, +
is not used for string concatenation. To concatenate strings, use ::
Subtraction
The subtraction operator -
subtracts one number from another.
5 <!-- 2 -->
Multiplication
The multiplication operator *
multiplies two numbers together.
4 <!-- 8 -->
Division
The division operator /
divides one number by another.
8 <!-- 4 -->
Modulus
The modulus operator %
returns the remainder of a division operation.
123 <!-- 23 -->
String operators
Concatenation
The concatenation operator ::
combines two strings together.
Hello, <!-- "Hello, World!" -->
Concatenation can be used to combine strings with other types, which will be converted to string.
The answer is: <!-- "The answer is: 42" -->
Logical operators
Logical operators are used to combine or modify boolean values.
And
The logical AND operator &&
returns true if both operands are true.
true <!-- true -->
true <!-- false -->
Or
The logical OR operator ||
returns true if at least one operand is true.
true <!-- true -->
false <!-- false -->
Not
The logical NOT operator !
negates a boolean value.
<!-- false -->
<!-- true -->
NOT operator converts non-boolean values to boolean first. Double negation !!
can be used to convert any value to
boolean.
<!-- false -->
<!-- true -->
<!-- false -->
<!-- true -->
Comparison operators
Comparison operators are used to compare two values.
Equal
The equality operator ==
returns true if the operands are equal.
42 <!-- true -->
text<!-- true -->
42 <!-- true, type coercion -->
42 <!-- false -->
Not equal
The inequality operator !=
returns true if the operands are not equal.
42 <!-- true -->
text<!-- false -->
42 <!-- false, type coercion -->
greater than
The greater than operator >
returns true if the left operand is greater than the right operand.
5 <!-- true -->
3 <!-- false -->
5 <!-- false -->
less than
The less than operator <
returns true if the left operand is less than the right operand.
3 <!-- true -->
5 <!-- false -->
5 <!-- false -->
greater than or equal to
The greater than or equal to operator >=
returns true if the left operand is greater than or equal to the right
operand.
5 <!-- true -->
3 <!-- false -->
5 <!-- true -->
less than or equal to
The less than or equal to operator <=
returns true if the left operand is less than or equal to the right operand.
3 <!-- true -->
5 <!-- false -->
5 <!-- true -->
Null-coalescing operator
The null-coalescing operator ??
returns the left operand if it is not null, otherwise it returns the right operand.
If variable
is null, the expression will return 'default value'
. If variable
is not null, it will return its
value.
Ternary operator
The ternary operator ? :
is a shorthand for an if-else statement. It takes three operands: a condition, a value if the
condition is true, and a value if the condition is false.
If condition
is true, the expression will return valueIfTrue
. If condition
is false, it will return
valueIfFalse
.
Parentheses
Parentheses ()
can be used to group expressions and control the order of evaluation.
<!-- 12 -->
2 <!-- 8 -->
Expressions inside parentheses are evaluated first.
Operator precedence
When multiple operators are used in an expression, operator precedence determines the order in which the operators are
evaluated. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the
same precedence, they are evaluated from left to right.
The following table lists the operators in order of precedence, from highest to lowest:
Precedence | Operator | Description |
---|---|---|
1 | () |
Parentheses |
3 | * , / , % |
Multiplication, Division, Modulus |
4 | + , - |
Addition, Subtraction |