Skip to content

cannot traverse an already closed generator #3063

@ricohumme

Description

@ricohumme

This behavior occurs when applying the following:

  1. Filter a collection in twig and store the Generator in a variable
  2. Check the Generator having iterable content by applying the filter length
  3. Iterate the Generator in a for loop in twig

In some cases there is nothing to iterate over, so by checking the size of the Generator and seeing there is something to iterate over, I render some content previous to the for loop and then iterate the Generator.

However, the Generator is being closed after applying the filter length, rendering the Generator useless after that.

Example:

    {% set collection = ['property1', 'property2'] %}
    {% set generator = collection|filter(v => v is same as('property1')) %}
    {% if generator|length > 0 %}
        {% for item in generator %}
            {{ item }}
        {% endfor %}
    {% endif %}

Is this intent?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions