TinyTemplate
TinyTemplate copied to clipboard
Make formatters work with @-keywords
My use-case for formatters on @-keywords is that I need to compute the previous/next index in a loop.
In the template I have the following:
{{ for graph in graphs }}
<div>
<a href=" #graph-{@index|prev}">previous</a>
<a href=" #graph-{@index|next}">next</a>
<div class="graph">...</div>
</div>
{{ endfor }}
and in the template compiler something like:
tt.add_formatter("prev", |value, output| {
let value = match value {
Value::Number(n) => n.as_i64().unwrap(),
_ => unreachable!(),
};
write!(output, "{}", value - 1)?;
Ok(())
});
Another way around this would be to add "next"/"prev" fields to each "graph", but it seems pretty verbose?