Comments (8)
Yeah no, variables in Stencil are scoped within for
tags (amongst others, we also have macro
blocks in StencilSwiftKit). That means that whatever changes you make during a for
loop, won't be accessible outside of that loop.
What you can always do is add an if
test inside the for
loop and check what the value of forloop.last
is.
from stencilswiftkit.
@djbe thanks for the quick response!
{% forloop.last %}
seems to be a Boolean
valued indicator of the whether this is the last execution of the for loop. I think I need access to the last value of the for loop variable so I can have the current and last values at the same time. How would you generate the following?
// source file
enum ABC {
case a,b,c
}
// generated file
func reduce(first: ABC, second: ABC) {
switch (first, second) {
case (.a, .b): print("ab")
case (.b, .c): print("bc")
default: print("unhandled")
}
}
from stencilswiftkit.
@djbe I thought {% set lastCaseName %}{{ case.name }}{% endset %}
would update the value of the lastCaseName
within the scope of the enclosing for
loop. But it does not. This seems a bug to me...
from stencilswiftkit.
No, each iteration of the for loop is a new scope.
I now understand what you're trying to achieve. I don't think there's an easy built-in way to achieve this, unfortunately. But, if you don't mind some bizarre code (untested):
{% for case1 in items %}
{% if not forloop.first %}
{% set firstLoopIndex %}forloop.counter{% endset %}
{% for case2 in items %}
{% if forloop.counter0 == firstLoopIndex %}
// do stuff
// {{ case1 }}, {{ case2 }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
from stencilswiftkit.
{% set firstLoopIndex %}forloop.counter{% endset %}
produces bad output.
Did you mean {% set firstLoopIndex %}{{ forloop.counter }}{% endset %}
?
Also, is forloop.counter0
a syntax error?
from stencilswiftkit.
Yeah I forgot the {{}} tags
Sent with GitHawk
from stencilswiftkit.
@djbe thanks again for your response! Very much appreciated
For anyone else encountering this scenario, beware: Sourcery versions < 11 do not support forloop.counter0
. Luckily for me, Sourcery v11 was released 7 hours ago
I would also suggest that special mention should be made in the docs that a for loop introduces a new scope with each iteration. This is highly irregular, and break the notion of what a scope is. This is surely a Stencil
shortcoming, and needs to be explicitly called out.
from stencilswiftkit.
Hi @DanielAsher
I'd suggest you open an issue in the Stencil repo directly about that missing documentation about for loops creating new scopes on each iteration. You'd have better chances that the author of Stencil would see it and process it
from stencilswiftkit.
Related Issues (20)
- Change our filters to accept a `mode` parameter instead of a boolean.
- SwiftIdentifier filter: add modes for more flexibility
- Replace filter: unescape backslashes HOT 7
- Map only works with strings HOT 14
- Make `Filter`s public HOT 2
- Test Swift 4.2 (or drop support for it) HOT 4
- swiftIdentifier converts all caps to title case HOT 3
- new release
- HEX Data for Colors.xcassets HOT 1
- question: is map supported for dictionaries? HOT 1
- Adding spm dependency on StencilSwiftKit causes warning in project, soon to be error HOT 2
- question: how to combine keys of multiple dictionaries in a single list and remove duplicates
- How can I keep {{ }} in stencil?
- Mutations inside forloop not visible outside HOT 3
- Calling macro with a parameter invoking a filter crashes HOT 4
- Is it possible to use it with Sourcery? HOT 1
- reusable macro templates HOT 4
- Build failure using Swift 4.2 from Xcode 10 Beta HOT 4
- Release 2.6.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stencilswiftkit.