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

Setting and updating variables in loop #759

Closed
NielDuysters opened this issue Jan 5, 2023 · 5 comments
Closed

Setting and updating variables in loop #759

NielDuysters opened this issue Jan 5, 2023 · 5 comments

Comments

@NielDuysters
Copy link

I have a loop which outputs some items. However, if no items are outputted (because there are none) I want to show "no items". Basically I need to be able to set and update variables in Askama.

My code:

<div id="foodmenu">
            {% for category in categories %}
            
                //PSEUDO
                {% set hash_dishes = False %}

                <div class="foodmenu-category">
                    <span class="foodmenu-category-title">{{ helpers::capitalize(category.1) }}</span>
                    <div class="foodmenu-item-container">
                        {% for dish in restaurant.dishes %}
                            {% if !dish.is_disabled %}

                                /////////////////////////////////
                                {% hash_dishes = True %}
                            
                                {% if dish.category.is_some() && dish.category.unwrap() == category.0 %}
                                    <div class="foodmenu-item" data-id="{{ dish.id }}">
                                        <span class="foodmenu-item-name">{{ dish.name }} <img alt="Toevoegen" class="foodmenu-item-add" src="/assets/media/images/icons/add.png"></span>
                                        <span class="foodmenu-item-description">{{ dish.description }} <img alt="(?)" class="foodmenu-item-description-info" src="/assets/media/images/icons/question-mark.png"></span>
                                        <span class="foodmenu-item-price">&euro; {{ format!("{:.2}", dish.variations[0].price).replace(".", ",") }}</span>
                                        {% if dish.image.is_some() %}
                                            <img alt="" class="foodmenu-item-image" src="{{ dish.image.as_ref().unwrap() }}">
                                        {% endif %}
                                    </div>
                                {% endif %}
                            {% endif %}
                        {% endfor %}

                        ///////////////////////
                        {% if !hash_dishes %}
                            No dishes.
                        {% endif %}

                    </div>
                </div>
            {% endfor %}
        </div>
@Kijewski
Copy link
Contributor

Kijewski commented Jan 5, 2023

You could probably use for-else: #518

{% for dish in restaurant.dishes if !dish.is_disabled %}
    {% if dish.category.is_some() && dish.category.unwrap() == category.0 %}
        ...
    {% endif %}
{% else %}
    No dishes.
{% endfor %}

@NielDuysters
Copy link
Author

I know about for-else but that is not possible in this case. I loop over all the categories, and inside that loop I go over all the dishes, check the category of each dish AND if dish.category == category_of_loop I output the dish.

@Kijewski
Copy link
Contributor

Kijewski commented Jan 5, 2023

Then you could use a Cell:

{% set hash_dishes = std::cell::Cell::new(false) %}

{% for dishes in restaurant %}
    {% for dish in dishes %}
        {% if dish.is_tasty() %}
            {% let _ = hash_dishes.set(true) %}
            dish
        {% endif %}
    {% endfor %}
{% endfor %}

{% if !hash_dishes.get() %}
    no dishes
{% endif %}

@NielDuysters
Copy link
Author

Thanks a lot! This works perfectly.

@Kijewski
Copy link
Contributor

Kijewski commented Jan 5, 2023

You're welcome! :)

@Kijewski Kijewski closed this as completed Jan 5, 2023
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

2 participants