Skip to content

Conversation

@hhirtz
Copy link

@hhirtz hhirtz commented Apr 23, 2021

No description provided.

@hhirtz
Copy link
Author

hhirtz commented May 5, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant