Skip to content

Support iterating over maps/objects #38

@jamespfennell

Description

@jamespfennell

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions