Introduction
Textwire is a domain-specific language (DSL) tailored for Go projects as a templating language, designed to embed dynamic content into text-based formats like HTML, XML, JSON, or any other format.
Built specifically for Go, Textwire offers a clean and intuitive syntax that simplifies the injection of variables and logic into any text-based format.
Syntax Highlighting
If you use Neovim or VSCode code editor, you can use our Neovim plugin or VSCode extension to get syntax highlighting and other features for Textwire.
Textwire enables the integration of dynamic content into HTML files to create data-driven, responsive pages.
The syntax is designed to be familiar and easy-to-learn, particularly for developers experienced with other template languages. Visit Language Elements page to explore the full range of available statements and directives for your templates.
How to Use It
You can use Textwire in three different ways:
- As a templating engine for web applications
- To embed dynamic content into strings
- To embed dynamic content into files
Below is a simple example of a Textwire template:
@use('~main')
@insert('title', 'Welcome to Home Page')
@insert('content')
<h1>Welcome to Textwire</h1>
<p>Our team, along with {{ user.name }}, is pleased to welcome you!</p>
<div>
@each(book in books)
@component('~book', { book })
@else
<h2>No books found</h2>
@end
</div>
@endMathematical Expressions
Textwire operates similarly to Go when it comes to mathematical expressions. You can use the following operators in your templates:
- Addition:
+(a + b) - Subtraction:
-(a - b) - Multiplication:
*(a * b) - Division:
/(a / b) - Modulus:
%(a % b) - Parentheses:
()((a + b) * c) - Greater than:
>(a > b) - Less than:
<(a < b) - Greater than or equal to:
>=(a >= b) - Less than or equal to:
<=(a <= b) - Equal to:
==(a == b) - Not equal to:
!=(a != b) - Increment:
++(a++) - Decrement:
--(a--) - Negation:
-(-a) - Logical NOT:
!(!a) - Logical AND:
&&(a && b) - Logical OR:
||(a || b)
Type Mismatching
Type mismatching is not supported when performing mathematical operations. For example, you cannot add an integer to a float directly. Type conversion is required to match types using conversion functions such as <your float>.int(), <your int>.float(), etc.
Reserved Variable Names
Textwire has two reserved variable names that you cannot use:
loopObject that is used inside loops. Read moreglobalGlobal object available in all Textwire files. Read more
Truthy and Falsy Values
Textwire evaluates values as truthy or falsy based on their type when they are used as boolean:
| Type | Truthy Value | Falsy Value |
|---|---|---|
| boolean | true | false |
| string | Non-empty string | Empty string ("") |
| integer | Non-zero integer | 0 |
| float | Non-zero float | 0.0 |
| array | always true | always true |
| object | always true | always true |
| nil | always false | always false |
This means you can use any literal value in logical expressions:
{{ "nice" && 13 ? "Yes" : "No" }}In the example above, the result is "Yes" because:
"nice"is a non-empty string, so it's truthy13is a non-zero integer, so it's truthy- Both values are truthy, so the logical AND returns the last truthy value (
13), which evaluates totruein the ternary expression
