macro_rules! ctx {
($($key:ident : $val:tt),* $(,)?) => { ... };
}Expand description
Construct a Context with JSON-like syntax.
Values are recursively converted:
"string"→Value::Str42_i64→Value::Inttrue/false→Value::Bool[a, b, c]→Value::List{ key: val, ... }→Value::Struct(expr)→ any expression viaInto<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");