Crate minilate

Crate minilate 

Source
Expand description

ยงMinilate

Crates.io Documentation Tests License

A templating engine prioritising minimal binary size and speed at the cost of feature set.

ยง๐Ÿš€ Quick Start

use minilate::{MinilateEngine, Context, VariableTy, MinilateInterface};

// Create engine and add template
let mut engine = MinilateEngine::new();
engine.add_template("hello", "Hello, {{ name }}!").unwrap();

// Create context
let mut context = Context::new();
context.insert("name", VariableTy::String.with_data("World".to_string()));

// Render
let result = engine.render("hello", Some(&context)).unwrap();
assert_eq!(result, "Hello, World!");

ยงโšก Performance

Minilate is designed for only a core feature set, but maximised performance and minimised binary size.

EngineBinary SizeRel. SizeTime/TemplateRel. Perf.
Minilate1.56 MB1.00x205.08 ยตs1.00x
Handlebars1.75 MB1.12x776.79 ยตs3.79x
MiniJinja2.00 MB1.28x438.03 ยตs2.154

๐Ÿ“Š Minilate achieves 12-28% smaller binaries while being ~2-4x faster than alternatives.

Benchmarks: 100 complex templates ร— 50 iterations each. Template includes conditionals, loops, and nested data. See Benchmarking for details.

ยง๐Ÿš€ Features

  • Simple replacements using {{ variable }}
  • Conditional blocks with boolean logic using {{% if <condition> %}}, {{% else if %}}, and {{% else %}}
    • NOT: !
    • AND: &&
    • OR: ||
  • For loops with {{% for var in iterable %}}
  • Nested template injection using {{<< <template_file_name>.tmpl }}
  • Escaping with \{{ or \{{%

ยง๐Ÿ› ๏ธ Installation

Add this to your Cargo.toml:

[dependencies]
minilate = "0.1"

ยง๐Ÿ“– Examples

ยงBasic Variable Substitution

Welcome to {{ site_name }}!
Your username is {{ user.name }}.

ยงConditional Logic

{{% if user && is_active %}}
  Hello, {{ user }}! You have {{ message_count }} new messages.
{{% elif !is_active %}}
  Your account is inactive. Please contact support.
{{% else %}}
  Hello, Guest! Please log in to continue.
{{% endif %}}

ยงLoops and Nested Data

# Shopping Cart
{{% if items %}}
Total items: {{ item_count }}

{{% for item in items %}}
- {{ item.name }} ({{ item.quantity }}x) - ${{ item.price }}
  {{% if item.special %}}**ON SALE!**{{% endif %}}
{{% endfor %}}

Total: ${{ total_price }}
{{% else %}}
Your cart is empty.
{{% endif %}}

ยงTemplate Inclusion

{{<< header.tmpl }}

# Main Content
{{ content }}

{{<< footer.tmpl }}

ยงComplex Example

# User Profile: {{ user.name }}

{{% if user.active %}}
**Status:** Active User (Age: {{ user.age }})

{{% if show_details && has_access %}}
## Recent Activity
{{% for activity in recent_activities %}}
- {{ activity.date }}: {{ activity.description }}
  {{% if activity.important %}}โš ๏ธ **Important**{{% endif %}}
{{% endfor %}}
{{% elif !has_access %}}
*You don't have permission to view activity details*
{{% endif %}}
{{% else %}}
**Status:** Inactive User
{{% endif %}}

ยง๐Ÿ“Š Benchmarking

To run benchmarks locally:

./scripts/benchmark.sh

This script:

  • Builds all benchmark binaries
  • Runs performance tests against Minilate, Handlebars, and MiniJinja
  • Analyzes binary sizes
  • Compares results with previous runs
  • Saves results for historical tracking

The benchmark tests template rendering with:

  • 100 different contexts
  • 50 iterations per engine
  • Complex templates with conditionals, loops, and nested data

ยง๐Ÿ“„ License

Licensed under either of

at your option.

ยง๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Structsยง

Context
Holds the data (variables) available during template rendering.
MinilateEngine
MinilateEngine is the primary implementation of the MinilateInterface trait, providing a complete templating engine for the Minilate system
Template
A Template represents a parsed template that can be rendered with a context.
Variable

Enumsยง

MinilateError
Represents errors that can occur during the operation of the Minilate template engine.
VariableTy
Specifies the type of a variable within a Context.

Traitsยง

MinilateInterface
MinilateEngine is a trait for the Minilate templating engine, an opinionated and minamalistic templating engine designed for use in offline, static, single-threaded environments.