3 unstable releases

Uses old Rust 2015

0.2.0 Feb 13, 2017
0.1.1 Nov 22, 2016
0.1.0 Nov 21, 2016

#16 in #render-template

Download history 3/week @ 2024-02-19 27/week @ 2024-02-26 1/week @ 2024-03-25 57/week @ 2024-04-01

58 downloads per month
Used in ocean

MIT license

47KB
1K SLoC

mage

Project Status: Active - The project has reached a stable, usable state and is being actively developed. docs

Mage is an intuitive and powerful template engine.

Document

Usage

Add dependency to Cargo.toml

[dependencies]
mage = "^0.2"

In your main.rs or lib.rs:

extern crate mage;

Supported keywords: for if include set

Mage support expression evaluate with crate eval. Built-in functions and operators documentation: eval.

License

mage is primarily distributed under the terms of the MIT license. See LICENSE for details.


lib.rs:

Mage is an intuitive and powerful template engine.

Supported keywords: for if include set

Mage support expression evaluate with crate eval. Built-in functions and operators documentation: eval.

Examples

Render template from string:

use mage::Template;

let source = "{{if true}}Hello world!{{if}}";
let output = Template::new().template(source).render().unwrap();
assert_eq!(output, "Hello world!");

Render template from file:

use mage::Template;

let output = Template::new()
    .root("examples")
    .extension("html")
    .open("main")
    .unwrap()
    .render()
    .unwrap();

Keywords

For

Loop a value.

{{for value in object}}
    {{value}}
{{for}}

{{for value, key in object}}
    {{key}}
    {{value}}
{{for}}

If

Conditionally render a block.

{{if boolean}}
    Hello world!
{{elseif boolean}}
    Hello world!
{{elseif boolean}}
    Hello world!
{{else}}
    Hello world!
{{if}}

Include

Include a sub template.

{{include views/header}}

Set

Set a value to current context.

{{set boolean=true}}
{{set number=123}}
{{set string='Hello world!'}}

Dependencies

~0.8–1.2MB
~25K SLoC