Git Product home page Git Product logo

Comments (8)

djbe avatar djbe commented on June 12, 2024

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.

DanielAsher avatar DanielAsher commented on June 12, 2024

@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.

DanielAsher avatar DanielAsher commented on June 12, 2024

@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.

djbe avatar djbe commented on June 12, 2024

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.

DanielAsher avatar DanielAsher commented on June 12, 2024

{% 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.

djbe avatar djbe commented on June 12, 2024

Yeah I forgot the {{}} tags πŸ˜…. The counter0 is meant to be there, it’s a 0-based index instead of 1-based (see stencil docs)

Sent with GitHawk

from stencilswiftkit.

DanielAsher avatar DanielAsher commented on June 12, 2024

@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.

AliSoftware avatar AliSoftware commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.