Literals
- String
{{ "Hello, World!" }}or{{ 'Hello, World!' }} - Integer
{{ 123 }}or{{ -234 }} - Float
{{ 123.456 }} - Boolean
{{ true }} - Nil
{{ nil }} - Array
{{ [1, 2, 3] }} - Object
{{ { "name": "John", "age": 25 } }}
String
You can use string literals and concatenate them with other strings. You can use double or single quotes for strings. Example:
{{ "Hello" + 'World!' }}When you print a string, it will be automatically escaped. If you want to print a string without escaping it, you can use the raw embedding with {!! and !!}. Example:
{!! "<h1>Test</h1>" !!}Important Notes
- Auto-escaped on output. All strings are automatically HTML-escaped when printed to prevent XSS attacks.
- Use
{!! !!}for unescaped output. Use the{!! myVar !!}when you need to output HTML, XML, JSON without escaping. - Concatenation with
+. Use the+operator to join strings together. - Quote flexibility. Single and double quotes are interchangeable for string literals.
- Escaping. Use a backslash to insert special characters inside strings. See Escaping.
Integer
You can use integer literals and perform arithmetic operations with them. Example:
<span>{{ 1 + 2 }}</span>Important Notes
- Arithmetic operators. Support
+,-,*,/,%operations. - Division returns an integer. Integer division like
4 / 2returns2(integer). Use a float literal if you need a float result, for example4.0 / 2.0. - Negative numbers. Prefix with
-for negative integers (e.g.,-42). - Overflow. Integers are 64-bit. Adding to the maximum value
9223372036854775807overflows and wraps around to-9223372036854775808. Example:textwire{{ 9223372036854775807 + 1 }} {{-- Output: -9223372036854775808 --}}
Nil
nil represents the absence of a value. It is commonly used to indicate that a variable has no value assigned or that something doesn't exist.
In Textwire, nil is treated as false in boolean contexts, making it easy to check for missing values:
{{ user = nil }}
@if(!user)
<p>No user found</p>
@endImportant Notes
- Converts to
falsein conditions.nilis falsy in boolean contexts. - Prints as empty string. Outputting
nildisplays nothing. - Array out-of-bounds returns
nil. Accessing non-existent array indices returnsnilinstead of error. - Accessing object property
nil. Accessing a property that doesn't exist on object returnsnilinstead of error.
Float
You can use float literals and perform arithmetic operations with them. Example:
<span>{{ 1.534 + 2.5 }}</span>Important Notes
- Precision limit (IEEE 754). Floating-point numbers have ~15-17 digit precision. For example,
1234567890.1234567890rounds to1234567890.1234567. - Large numbers as strings. Keep large numbers as strings if you need exact precision.
- Division follows the operand types. Float division like
4.0 / 2.0returns a float. Integer division like4 / 2returns an integer. - Follows IEEE 754 like Go. Overflow produces
+Infor-Inf, values too small to represent underflow to0.0, and invalid operations like+Inf - +InfproduceNaN. These special values are printed as+Inf,-Inf, andNaN.
- Non-finite floats become
nullin JSON.+Inf,-Inf, andNaNare serialized asnull.
Boolean
Booleans represent truth values and can be either true or false. They are essential for controlling program flow with conditional statements.
{{ isAdmin = true }}
@if(isAdmin)
<button>Delete User</button>
@else
<p>You don't have permission</p>
@endImportant Notes
- Truthy values. Non-empty strings, non-zero numbers (including negatives), non-empty arrays, and objects with at least one key are truthy.
- Falsy values.
false,nil,0,0.0, empty strings"", empty arrays[], and empty objects{}are falsy. - Printed as
1and0. Outputting a boolean renders1fortrueand0forfalse. Use a ternary expression if you need"true"or"false"text.
Array
Defining an array in Textwire is done in a similar way as in other languages. Example:
{{ names = ["John", "Jane", "Jack"] }}
<ul>
@each(name in names)
<li>{{ name }}</li>
@end
</ul>You can access values in an array by using an index. Example:
{{ names = ["John", "Jane", "Jack"] }}
<ul>
<li>{{ names[0] }}</li> {{-- John --}}
<li>{{ names[1] }}</li> {{-- Jane --}}
<li>{{ names[2] }}</li> {{-- Jack --}}
<li>{{ names[10] }}</li> {{-- nil --}}
</ul>Important Notes
- Out of bounds returns nil. Accessing an array at a non-existent index returns
nilinstead of resulting in an error. - Indexing strings. You cannot use
[number]to access a string character, instead, use at() function. - Prints as comma-separated. Printing an array converts it to comma separated values. Example:textwire
<span>{{ [1, 2, 3] }}</span>html<span>1, 2, 3</span> <!-- Output --> - Check for nil first. Always check array access with index for
nilbefore using it to prevent using functions onnilerrors. Example:textwire{{ names = [] }} {{ names[0] ? names[0].upper() : '' }}
Object
Objects in Textwire are very similar to JavaScript object with key-value pairs. Example:
{{ person = {"name": "John", "age": 25} }}You can also use key names without quotes if your keys are valid identifiers:
{{ person = { name: "John", age: 25 } }}You can access values in an object by using a key. Example:
{{ user = {age: 25, name: {first: "Anna", last: "Cho"}} }}
<ul>
<li>First name: {{ user.name.first }}</li> {{-- "Anna" --}}
<li>Last name: {{ user.name.last }}</li> {{-- "Cho" --}}
<li>Age: {{ user.age }}</li> {{-- 25 --}}
</ul>You can also use an index expression with the key as a string:
{{ hero = { name: 'Skirk', 'release-version': '5.7' } }}
{{ hero['name'] }} {{-- Skirk --}}
{{ hero['release-version'] }} {{-- 5.7 --}}This is useful for keys that are not valid identifiers, such as release-version.
Important Notes
- First character case-insensitivity in field access. Field name matching ignores case differences in the first character. This means
{{ user.name.first }}and{{ user.Name.First }}resolve to the same result. - Keys must be identifiers or strings. Unquoted keys have to be valid identifiers. If a key contains characters like
-or starts with a number, wrap it in quotes. Example:textwire{{ user = { name: "John", "first-name": "Johnny", "1st": true } }} - Dot and bracket access. Use
obj.keyorobj['key']to read a value. Bracket access takes a string key. Use dot notation to assign values, for exampleuser.name = "Anna". - Safe accessing. You can use the get() function to safely access object properties without worrying about errors. Example:textwire
{{ user = {age: 25, name: {first: 'Anna', last: 'Cho'}} }} {{ user.get('name.first') }} - Only exported fields are converted. Textwire automatically converts Go structs to objects, but only exported fields are converted. Since Go doesn't export fields that start with lowercase letters, Textwire cannot access them. Make sure to capitalize field names if you want them available in your templates.
- Accessing missing object properties returns
nil. Similar to arrays, trying to access a property that doesn't exist on an object returnsnilrather than throwing an error. However, this only applies to the first undefined property—attempting to access a property onnilitself will result in an error. - Sorted keys when printed. Objects in Textwire maintain the order of keys in alphabetical order when printed or in JSON string if you do
{{ myObj.json() }}. - Shorthand Property Notation. Similar to objects in JavaScript, you can use shorthand property notation to define an object.textwire
{{ name = "John"; age = 25 }} {{ person = { name, age } }}
