@tsomaiatech/moxite
v1.0.5
Published
Moxite template language
Maintainers
Readme
Moxite Template Engine
Moxite is a fast, structural, and strictly evaluated template engine designed to enforce a clear separation between application logic and the presentation layer.
Moxite approaches template parsing through a dedicated, character-by-character Lexer and a recursive descent Abstract Syntax Tree (AST) Parser, moving away from string-replacement or Regex-heavy architectures.
By evaluating an isolated AST, Moxite cleanly scopes template logic and ensures high reliability when parsing complex, overlapping data structures.
Design Philosophy
The core philosophy of Moxite is to keep templates simple and focused purely on rendering:
- Explicit Lexing: Every template token is mapped through an atomic Lexical State machine and evaluated via a recursive descent Parser, ensuring predictable boundaries even when templates overlap with other syntaxes like JSON, bash scripts, or nested code blocks.
- Strict Expression Evaluation: The expression parser deliberately limits complex inline operations. Features like inline array definitions
[1, 2]or JSON closures{key: val}are intentionally unsupported, encouraging developers to construct their data payloads cleanly in the upstream application code. - Implicit Safe Navigation: The engine automatically protects against
nullorundefinedproperty access without throwing verbose errors. For example,{{ user.profile.name }}is implicitly handled asuser?.profile?.nameunder the hood.
Syntax Outline
Interpolation & Implicit Chaining
Hello, {{ user.name }}!
Implicit Safe Output: {{ user.deep.missing.path.shouldNotCrash }}Conditionals (@if)
@if (user.isAdmin)
Welcome Administrator.
@else if (user.isGuest === "yes")
Welcome Guest.
@else
Welcome User.
@endifLoops (@for)
@for (item of items)
Item: {{ item.name }}
@else
No items found.
@endforMoxite supports an optional @else fallback inside @for loops, which executes automatically if the iterable is empty or undefined.
Additionally, Loop Metadata is injected into the scope for every iteration, allowing you to access:
{{ $index }}(0-based iteration index){{ $first }}(boolean){{ $last }}(boolean){{ $count }}(total length of the array)
Block Scoping (@const)
@const greeting = "Hello"
@const role = user.role
{{ greeting }}, you are a {{ role }}Pipes (|)
Pipes allow you to pass variables through formatting functions registered in your evaluation context.
{{ user.name | upper }}
{{ total | currency: "USD" }}Components & Hoisting (@template / @use)
Moxite supports creating reusable local template blocks. Template definitions are hoisted, meaning you can @use them before defining them in the file:
@for (user of users)
{{ @use card with { user } }}
@endfor
@template card({ user })
<div class="card">{{ user.name }}</div>
@endtemplateRaw Blocks (@raw)
Content inside a @raw block is completely ignored by the parser and output exactly as-is. Useful for emitting strings like {{ or @ that would otherwise trigger the template engine.
@raw
{{ this.will.not.be.parsed }}
@endrawClean Output (Whitespace Stripping)
Moxite automatically trims structural whitespace. When you write structural tags like @if, @for, or @const on their own lines, Moxite will strip the indentation and trailing newlines so that the final output remains perfectly clean, without injecting empty blank lines everywhere. Content inside standard text blocks remains entirely unmodified.
Structure
src/: Contains the pure TypeScript reference implementation of the strict Lexer, AST Parser, and Evaluator (template-engine.ts).idea-plugin/: A Gradle project containing the IntelliJ IDEA plugin. Powered by Grammar-Kit and JFlex, this plugin provides native JetBrains syntax highlighting, completion, and semantic validation for.mxfiles using the exact same DFA token boundaries as the TypeScript engine.
