MoonTera: Full-Featured Template Parser & Rendering Engine for MoonBit
MoonTera is a robust, dynamic, and extensible template engine written from scratch in native MoonBit. Inspired by Python’s Jinja2 and Rust’s Tera, MoonTera parses text templates into an Abstract Syntax Tree (AST) at runtime and dynamically evaluates them against a context environment.
It is designed for server-side HTML rendering, configuration file generation (YAML/JSON/TOML), email templates, and code generator scripts.
🌟 Core Features
Lexer & Parser (AST): A robust tokenizer and a recursive-descent expression parser with mathematical and comparative operator precedence.
Variable Interpolation: Seamless variable access {{ user.name }} with support for dynamic types.
MoonTera: Full-Featured Template Parser & Rendering Engine for MoonBit
MoonTera is a robust, dynamic, and extensible template engine written from scratch in native MoonBit. Inspired by Python’s Jinja2 and Rust’s Tera, MoonTera parses text templates into an Abstract Syntax Tree (AST) at runtime and dynamically evaluates them against a context environment.
It is designed for server-side HTML rendering, configuration file generation (YAML/JSON/TOML), email templates, and code generator scripts.
🌟 Core Features
{{ user.name }}with support for dynamic types.+,-,*,/,%), comparison operators (==,!=,<,<=,>,>=), and logical short-circuiting (and,or,not).new_child) for local loop variables.{% if %}/{% else %}) and loop iterations ({% for item in items %}).{% extends "base.html" %}and overridden block placeholders{% block main %} ... {% endblock %}.{{ name | upper }}with arguments:upper: Convert text to uppercase.lower: Convert text to lowercase.length: Get array or string length.default(value): Apply fallback value if string is empty or null.🏗️ Architecture Design
MoonTera is built on a modular pipeline ensuring clear separation of concerns:
src/value/value.mbt: Dynamic data representation supporting Null, Boolean, Number, String, Array, and Object.src/lexer/lexer.mbt: Custom lexical scanner including decimal/float tokenization.src/parser/parser.mbt: Syntactic parser resolving block tags and operators.src/context/context.mbt: Scoping layer resolving variable bindings.src/evaluator/evaluator.mbt: Traverses nodes and expressions to format text.src/filters/filters.mbt: Text formatting and mapping functions.src/engine/engine.mbt: Main API and Template Inheritance block merger.🚀 Quick Start
1. Basic Template Evaluation
2. Template Inheritance (Extends & Block)
🛠️ Running the Built-in Demo
MoonTera comes with a preconfigured command line entrypoint showing all features working together. You can run it locally with:
📂 Project Structure
📄 License
This project is licensed under the MIT License. See LICENSE for details.