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

How to get the specified value in the array #815

Closed
Yakumo-Yukari opened this issue Apr 24, 2023 · 4 comments
Closed

How to get the specified value in the array #815

Yakumo-Yukari opened this issue Apr 24, 2023 · 4 comments

Comments

@Yakumo-Yukari
Copy link

Yakumo-Yukari commented Apr 24, 2023

{% for image in post.images %}
    {% if image.len() == 0 %}
        No img
    {% else if image.len() == 1 %}
        <img src="{{image}}" class="image1">
    {% else if  image.len() >= 6  %}
        <img src="{{image 1}}" class="1">
        <img src="{{image 2}}" class="2">
        <img src="{{image 3}}" class="3">
    {% endif %}
{% endfor %}

For example, when the pictures in my array are larger than 6 pictures, I only need 3 of them. How can I get the value?

@Kijewski
Copy link
Contributor

Kijewski commented Apr 24, 2023

Something like that should work:

{% for image in post.images %}
    {% if loop.index > 6 %}
        {% break %} {# break out of the loop #}
    {% endfor %}
    <img src="{{image_1}}" class="{{loop.index }}" />
{% else %}
    No img {# if the for-loop was never entered #}
{% endfor %}

@Yakumo-Yukari
Copy link
Author

Yakumo-Yukari commented Apr 25, 2023

Something like that should work:

{% for image in post.images %}
    {% if loop.index > 6 %}
        {% break %} {# break out of the loop #}
    {% endfor %}
    <img src="{{image_1}}" class="{{loop.index }}" />
{% else %}
    No img {# if the for-loop was never entered #}
{% endfor %}

It doesn't work, my images is an images: Vec<String> and when I run it with {% if loop.index %} and{{image_1}}it prompts an error

@djc
Copy link
Owner

djc commented Apr 25, 2023

Just use {{ image }} (if you're in a loop). You might also be able to use something like {{ post.images[1] }}.

@Yakumo-Yukari
Copy link
Author

Yakumo-Yukari commented Apr 26, 2023

Just use {{ image }} (if you're in a loop). You might also be able to use something like {{ post.images[1] }}.

            {% let image = post.images.len() %}
            {% if image >=6 %}
                {{post.images[1]}}
                No.2
                {{post.images[5]}}
                {% else %}
                No img
                {% endif %}

Thanks for your tip, I solved this problem by taking the length.

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