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

Support iterating over maps/objects #38

Open
jamespfennell opened this issue Jan 18, 2024 · 0 comments
Open

Support iterating over maps/objects #38

jamespfennell opened this issue Jan 18, 2024 · 0 comments

Comments

@jamespfennell
Copy link

Thank you for this very nice project. I love the simple and low-dependency philosophy.

Somewhat not in that vein: what would you think about supporting iteration over maps/objects? Currently iteration is restricted to arrays. Following the standard syntax in other templating languages, it could look something like this:

{{ for key, value in guests }}
The key is {key} and the value is {value}
{{ endfor }}

I poked around the code to see what the implementation would be like, and I think it would be straightforward (I would do it):

  • We would add a new instruction similar to PushIterationContext. Perhaps PushObjectIterationContext(Path<'template>, &'template str, &'template str) where the enum payload is (path to the map, key variable name, value variable name).

  • In the compiler, the parse_for function would be changed to split the key name on ,, and then construct either a array iteration spec (no commas), an object iteration spec (1 comma) or an error (2 or more commas).

  • The new instruction would have to be implemented in template.rs. This would follow array iteration, probably with a new ContextElement.

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

No branches or pull requests

1 participant