locomotivecms / steam

The rendering stack used by both Wagon and Station (new name of the engine). It includes the rack stack and the liquid drops/filters/tags.
MIT License
38 stars 59 forks source link

with_scope strange issue #194

Closed gcolson closed 2 years ago

gcolson commented 3 years ago

Hi,

I got stuck with a strange issue with the with_scope liquid tag. I'll try to explain it. I got two content types "positions" and "people", that have a many-to-many relation between them. "positions" has a select field called "category".

Now i have this piece of code to try to display each people for each positions scoped by the category field. For each person i try to display all of its positions. Here is what it looks like :

{% for cat in contents.positions.category_options %}
      <div id="{{ cat }}" class="page-part-section" {{ section.locomotive_attributes }}>
        <h1 class="title">{{ cat.name }}</h1>
        <div class="columns is-centered is-multiline">
          {% with_scope category: cat.name %}
            {% for position in contents.positions %}
                  {% for person in position.people %}
                      {% for p in person.positions %}
                        {% assign all_positions = all_positions | push: p.name %}
                      {% endfor %}
                      {% assign all_positions = all_positions | join: ', ' %}

                      <div class="column is-half">
                        <a href="{% path_to person %}">
                          --- Some code displaying the person with all its positions using the `all_positions` liquid variable.---
                        </a>
                      </div>
                  {% endfor %}
            {% endfor %}
          {% endwith_scope %}
        </div>
      </div>
{% endfor %}

The strange thing here is that using this code, all_positions is empty (even though my people are related to many positions). I just can't get the positions of a person. But if I don't use with_scope and replace it by an if, that works :

{% for cat in contents.positions.category_options %}
      <div id="{{ cat }}" class="page-part-section" {{ section.locomotive_attributes }}>
        <h1 class="title">{{ cat.name }}</h1>
        <div class="columns is-centered is-multiline">
         {% for position in contents.positions %}
              {% if position.category == cat.name %}
                  {% for person in position.people %}
                      {% for p in person.positions %}
                        {% assign all_positions = all_positions | push: p.name %}
                      {% endfor %}
                      {% assign all_positions = all_positions | join: ', ' %}

                      <div class="column is-half">
                        <a href="{% path_to person %}">
                          --- Some code displaying the person with all its positions using the `all_positions` liquid variable.---
                        </a>
                      </div>
                  {% endfor %}
               {% endif %}
            {% endfor %}
          {% endwith_scope %}
        </div>
      </div>
{% endfor %}

the {% if position.category == cat.name %} is the line that aimed to replace with_scope.

I've struggled a long time to understand this, so I wanted to report it. Maybe it's my code that is not quite accurate, but for all the researchs I've made it seems like a bug to me.

greyskin commented 3 years ago

Hi @gcolson,

A couple of things:


Unfortunately the documentation on with_scope is pretty sparse.

gcolson commented 2 years ago

Hi ! Thanks for your answer. Too late for me so i just used the if solution and that works fine. I'm sure what you propose here should work, maybe i'll try it someday when i'm bored ;)