minijinja

Macro render

Source
macro_rules! render {
    (
        in $env:expr,
        $tmpl:expr
        $(, $key:ident $(=> $value:expr)?)* $(,)?
    ) => { ... };
    (
        $tmpl:expr
        $(, $key:ident $(=> $value:expr)?)* $(,)?
    ) => { ... };
}
Expand description

A macro similar to format! but that uses MiniJinja for rendering.

This can be used to quickly render a MiniJinja template into a string without having to create an environment first which can be useful in some situations. Note however that the template is re-parsed every time the render! macro is called which is potentially slow.

There are two forms for this macro. The default form takes template source and context variables, the extended form also lets you provide a custom environment that should be used rather than a default one. The context variables are passed the same way as with the context! macro.

§Example

Passing context explicitly:

println!("{}", render!("Hello {{ name }}!", name => "World"));

Passing variables with the default name:

let name = "World";
println!("{}", render!("Hello {{ name }}!", name));

Passing an explicit environment:

let env = Environment::new();
println!("{}", render!(in env, "Hello {{ name }}!", name => "World"));

§Panics

This macro panics if the format string is an invalid template or the template evaluation failed.