StencilSwiftKit icon indicating copy to clipboard operation
StencilSwiftKit copied to clipboard

Mutations inside forloop not visible outside

Open kaygro opened this issue 8 years ago • 3 comments

When mutating a variable inside a for-loop, these changes get reverted on loop exit. This is quite unlike most programming languages. Unfortunately this makes you use more boilerplate code inside templates than necessary.

kaygro avatar Jan 31 '18 21:01 kaygro

The for loop tag is from the Stencil library itself. Stencil builds up a stack whenever a for loop is executed, or when another template is included. We've added our own tags that use this same stack mechanism, for example the macro tag (https://github.com/SwiftGen/StencilSwiftKit/blob/master/Documentation/tag-macro.md).

If you'd like to propose a change to the for tag, I'd recommend making an issue in the appropriate repository (https://github.com/kylef/Stencil/issues), as we can't override/modify existing Stencil tags. If you'd like to propose new tags/filters, we could discuss this here if appropriate.

djbe avatar Jan 31 '18 21:01 djbe

I did check the sources of the stencil library and currently they actually appear to implement override sematics for extensions by same tags. Though I'll do an experiment on this later.

kaygro avatar Feb 02 '18 07:02 kaygro

@kaygro while this is really unlike done in programming languages, this is inline with Jinja 2 behaviour, which I guess was the main inspiration for Stencil. It does though provide namespace feature to solve this issue. http://jinja.pocoo.org/docs/2.10/templates/

ilyapuchka avatar Apr 17 '18 10:04 ilyapuchka