Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip items loop #1216

Closed
rludgero opened this issue May 5, 2019 · 3 comments
Closed

Skip items loop #1216

rludgero opened this issue May 5, 2019 · 3 comments

Comments

@rludgero
Copy link

rludgero commented May 5, 2019

Hello everyone,

I'm trying to filter the loop, through the value of the property, but from what I see in nunjucks this option does not exist, or so I think.

As in nunjucks it is not possible to use break or continue in a loop, how i can filter the sequence during iteration, skipping the items?

Is possible to do it, if so, how can I implement it?

Is there a filter?

This is the sample code:

"data": [
    {
        "products_data": [
            {
                "retailer": "Super Offer",
                "description": "Description 1"
            },
            {
                "retailer": "Mega Offer",
                "description": "Description 2"
            },
            {
                "retailer": "Super Offer",
                "description": "Description 3"
            },
            {
                "retailer": "Offer",
                "description": "Description 4"
            }
        ]
    }
]
{% set retailer_title = 'Super Offer' %}

{% for items in data.products_data if items.retailer == retailer_title %}
    <h3>{{ items.retailer }}</h3>
    <p>{{ items.description }}</p>
{% else %}
    There is no offer of {{ retailer_title }} available.
{% endfor %}

Thanks in advance.

@lasithds
Copy link

As I understood you want to loop through the loop and print data if a condition is met. If not matches found print another text. It can be done with a proper logic with the available functions.

{% set retailer_title = 'Super Offer' %}
{% set no_matches = true %}
{% for item in data.products_data %}
    {% if item.retailer == retailer_title %}
        {% set no_matches = false %}
        <h3>{{ item.retailer }}</h3>
        <p>{{ item.description }}</p>
   {% endif %}
{% endfor %}
{% if no_matches %}
    There is no offer of {{ retailer_title }} available.
{% endif %}

@fdintino
Copy link
Collaborator

@rludgero This is not yet possible with the syntax you provided. The ticket to add loop filtering is #261 and the tracking ticket for nunjucks v4 (which would include this feature) is #1059.

Unfortunately, you'll need to use the workaround @lasithds provided above. I am actively working on adding this feature though.

Closing as a duplicate. I am waiting on feedback to #1059 (comment), if you have any thoughts on it that would be very helpful.

@rludgero
Copy link
Author

Hi @lasithds , this is what I wanted 😉 , it has worked great, thank you very much 👏 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants