Skip to main content

ctx

Macro ctx 

Source
macro_rules! ctx {
    ($($key:ident : $val:tt),* $(,)?) => { ... };
}
Expand description

Construct a Context with JSON-like syntax.

Values are recursively converted:

  • "string"Value::Str
  • 42_i64Value::Int
  • true / falseValue::Bool
  • [a, b, c]Value::List
  • { key: val, ... }Value::Struct
  • (expr) → any expression via Into<Value>

§Examples

Simple values:

use md_tmpl_core::{Template, ctx};

let tmpl = Template::from_source(
    "\
---
params: [greeting = str, name = str]
---
{{ greeting }}, {{ name }}!",
)
.unwrap();
let output = tmpl
    .render_ctx(&ctx! {
        greeting: "Hello",
        name: "world",
    })
    .unwrap();
assert_eq!(output, "Hello, world!");

Nested dicts and lists:

use md_tmpl_core::{Template, ctx};

let tmpl = Template::from_source(
    "\
---
params: [items = list(label = str)]
---
> {% for item in items %}

{{ item.label }}

> {% /for %}",
)
.unwrap();
let output = tmpl
    .render_ctx(&ctx! {
        items: [
            { label: "alpha" },
            { label: "beta" },
        ]
    })
    .unwrap();
assert_eq!(output, "alpha\nbeta\n");