目录

MoonTemplate

A lightweight, flexible, and extensible text template engine designed exclusively for MoonBit.

Introduction

MoonTemplate is a highly extensible text templating engine built from scratch in MoonBit. It provides developers with a robust tool to generate dynamic content, ranging from HTML pages and emails to source code generation.

Key Features

  • Zero Dependencies: Pure MoonBit implementation.
  • Robust Parsing: Custom-built Lexer and AST (Abstract Syntax Tree) Parser ensuring safety and speed.
  • Variable Interpolation: Easily inject data into templates using {{ variable }}.
  • Control Flow (If/Else): Conditional rendering logic with {% if cond %}.
  • Iteration (For Loops): Support for rendering lists and arrays using {% for item in iterable %}.
  • Filter System: Extensible architecture via pipes {{ name | uppercase }}.
  • CLI Ready: Includes a scaffolding for a Command Line Interface.

Architecture

MoonTemplate is divided into 4 core modules to maintain high cohesion and low coupling:

  1. lexer: Tokenizes the raw template string into a stream of semantic tokens.
  2. parser: Consumes the token stream to build an Abstract Syntax Tree (AST).
  3. ast: Defines the node structures (Text, Variable, If, For).
  4. engine: The rendering runtime that traverses the AST and interpolates context data.

Quick Start

1. Basic Variable Rendering

let template = "Hello, {{ user }}! Welcome to MoonBit."
let engine = Engine::new(template).unwrap()
let ctx = Map::new()
ctx.set("user", "Hero001")

let output = engine.render(ctx)
println(output) // Hello, Hero001! Welcome to MoonBit.

2. Using Conditionals

let template = "{% if is_admin %}Admin Panel{% endif %}"
let engine = Engine::new(template).unwrap()
let ctx = Map::new()
ctx.set("is_admin", "true")
println(engine.render(ctx)) // Admin Panel

3. Filters

let template = "Current status: {{ status | uppercase }}"
let engine = Engine::new(template).unwrap()
engine.register_filter("uppercase", fn(s) { s.to_upper() })
// ... render with ctx

CLI Usage

MoonTemplate includes a CLI tool for rendering templates directly from your terminal.

moon run src/cli -- template.txt

Contributing

Please see CONTRIBUTING.md for details on how to contribute, build, and test the project.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

关于

一个为 MoonBit 语言设计的轻量、灵活且可扩展的文本模板引擎。支持基本的变量插值、条件判断(If/Else)、循环控制(For)以及自定义过滤器的注册扩展。适用于代码生成器、静态网站生成和 CLI 工具动态输出等场景。

74.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号