Skip to content

Expressions

Ternary

You can use ternary expressions to conditionally render content. Example:

textwire
<span>{{ x == 1 ? "yes" : "no" }}</span>

The advantage of a "ternary expression" over an "if statement" is that it can be used inside of any other expressions.

Prefix

You can use prefix expressions to negate or invert a boolean value. Example:

textwire
<span>{{ !isTall ? "Not tall" : "Is tall" }}</span>
<span>{{ -x }}</span>

Infix

You can use infix expressions to perform arithmetic operations. Example:

textwire
<ul>
    <li>{{ x + y }}</li> {{-- Addition --}}
    <li>{{ x - y }}</li> {{-- Subtraction --}}
    <li>{{ x * y }}</li> {{-- Multiplication --}}
    <li>{{ x / y }}</li> {{-- Division --}}
    <li>{{ x % y }}</li> {{-- Modulo --}}
    <li>{{ x == y }}</li> {{-- Equality --}}
    <li>{{ x != y }}</li> {{-- Inequality --}}
    <li>{{ x && y }}</li> {{-- Logical AND --}}
    <li>{{ x || y }}</li> {{-- Logical OR --}}
    <li>{{ (x + 2) / (y * (4 - c)) }}</li> {{-- Grouped expressions --}}
</ul>

Infix Operators

OperatorDescriptionLeft/Right types
==Equalany
!=Not equalany
&&Logical ANDany
||Logical ORany
%Modulointeger
+Plusinteger, float, string
-Minusinteger, float
*Multiplyinteger, float
/Divideinteger, float
>Greaterinteger, float
<Lessinteger, float
>=Greater or equalinteger, float
<=Less or equalinteger, float

Important Notes

  • String concatenation. Use + operator to concatenate strings. Example: {{ "Hello, " + name }} will result in Hello, {name}.

Postfix

You can use postfix expressions to increment or decrement a variable. Example:

textwire
<span>{{ x++ }}</span> {{-- Increment --}}
<span>{{ x-- }}</span> {{-- Decrement --}}

Postrix Operators

OperatorDescriptionLeft/Right types
++Incrementinteger, float
--Decrementinteger, float

Comparison

Comparison expressions produce a boolean value. Example:

textwire
@if(x == 1)
    <p>x is 1</p>
@end

Comparison Operators

OperatorDescriptionLeft/Right types
==Equalany
!=Not equalany
>Greaterinteger, float
<Lessinteger, float
>=Greater or equalinteger, float
<=Less or equalinteger, float
  • Any type for equality operators. Unlike Go, in Textwire, operators ==, !=, && and || can be used with any types on left and right.
  • Compare arrays and objects. Use == and != to deeply compare arrays and objects. Example: {{ [1, 2] == [1, 2] }} will result in true because both arrays have the same elements in the same order. Similarly, {{ { name: "Chiori" } == { name: "Chiori" } }} will also result in true because both objects have the same key-value pairs. It works for deeply nested arrays and objects as well.

Function Calls

You can use function calls to call functions. Textwire has a few built-in functions that you can use in your templates.

Functions in Textwire are type-specific, which means that you can't call a function on a variable that is not of the same type as the function. For example, you can't call a split function on an integer variable.

Example:

textwire
{{ name.split(" ") }}

You can read more detail about built-in functions on the Functions Guide page.

Logical Expressions

You can use logical OR (||) and logical AND (&&) expressions to combine boolean values.

textwire
@if(admin || owner)
    @component('admin-popup')
@end

@if(isSunny && isWarm)
    @component('good-weather-widget')
@end

Logical expression accepts any literal value and converts it to boolean. Read about Truthy and Falsy Values.