stencilproject/Stencil

Support ranges in template for loops

bfad opened this issue · 2 comments

bfad commented

Ranges aren't working in for loops. For example:

env.renderTemplate(
  name: "example.html",
  context: ["test": 8...8]
)

My example.html template:

<ul>
    {% for num in test %}
        <li>{{num}}</li>
    {% empty %}
        <li>Error</li>
    {% endfor %}
</ul>

I would expect the rendered output to be a list with a single <li>8</li> in it, but instead I see <li>Error</li>. It would be nice not to have to create an array out of the CountableClosedRange<Int> in order to get it to work.

Environment:
MacOS High Sierra
Swift 4
Xcode 9.0 beta 2

kylef commented

I think that proposed feature additional would make sense. If you're interesting in contributing this feature a good start would be at https://github.com/kylef/Stencil/blob/89256b9/Sources/ForTag.swift#L93, I think catching the range here and coercing it to an array in values would implement this. Tests for the feature can be very similar to the one at https://github.com/kylef/Stencil/blob/89256b9/Tests/StencilTests/ForNodeSpec.swift#L30-L38.

bfad commented

@kylef I'm pretty new to swift, but I wouldn't mind taking a stab at it. Thanks for the pointers; I'll see about getting you a PR