Skip to content

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:

  1. As a templating engine for web applications
  2. To embed dynamic content into strings
  3. To embed dynamic content into files

Below is a simple example of a Textwire template:

textwire
@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>
@end

Unicode Support

Textwire fully embraces Unicode, making it ideal for international applications and multilingual content. Whether you're building websites in Chinese, Russian, Arabic, or mixing multiple languages, Textwire handles it seamlessly.

String Functions

All built-in functions properly handle Unicode characters. They count characters correctly (not bytes) and support any language:

textwire
{{ "我喜欢中国".len() }} <!-- output: 5 (characters, not bytes) -->
{{ "привет".at(2) }} <!-- output: и -->

Keep in mind that with emojis it could be tricky, because some emojis are represented by multiple Unicode code points. The example below shows that the string "👋🏽🌍" has a length of 3, because the waving hand emoji with medium skin tone (👋🏽) is represented by two code points, while the globe emoji (🌍) is represented by one code point:

textwire
{{ "👋🏽🌍".len() }} <!-- output: 3 -->

File Names

Directives that accept file names—like @component, @use, @insert, @reserve, and @slot—fully support Unicode paths:

textwire
@component('书', { name })
@use('♥️/главная')

No Unicode for Identifiers

Unicode characters cannot be used in variable names, function names, or object fields. Only ASCII letters (a-z, A-Z), digits (0-9), and underscores are allowed. For example, user_name is valid, but 用户名 and имя are not.

Mathematical 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:

  1. loop Object that is used inside loops. Read more
  2. global Global object available in all Textwire files. Read more