Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make formatters work with @-keywords #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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