TinyTemplate icon indicating copy to clipboard operation
TinyTemplate copied to clipboard

Make formatters work with @-keywords

Open hhirtz opened this issue 5 years ago • 1 comments

hhirtz avatar Apr 23 '21 08:04 hhirtz

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?

hhirtz avatar May 05 '21 07:05 hhirtz