Exclude image from for loop based on name

Hi,

I’ve a full width gallery on the top of my page that simply loops over all images as follows:

{% for image in page.media.images %}
     <div data-src="{{ image.url }}" data-thumb=""></div>
{% endfor %}

A few lines lower I’ve another image that is take from the page directory.

<div class="bit-25 profile-img right">    
   {% if profile_image %}
              {% if profile_image_file %}
                        {% set profile_image_media = page.media.images[profile_image_file] %}
              {% else %}
                        <img src="{{ url('theme://images/profile.jpg') }}" class="profile-image"/>
              {% endif %}
                        {{ profile_image_media.html('Title', 'Alt', 'Class') }}
     {% endif %}
</div>
--- 

As you can see I set the 'profile image' based on toggle. If yes, then check the field 'profile_image_file', where the user provided the name of the image it wants as profile image.

This all works perfectly fine.. the only problem is that the profile image, being located in the page directory is also loaded in my full width gallery on top of my page. 

Is there a way to exclude this profile image from the first for loop, based on the name provided in 'profile_image_file'?

maybe something like?

{% for image in page.media.images %}
{% if page.media = profile_image_file %}

{% endif %}


{% endfor %}

Im sure its possible with grav, but so far i've had no luck trying to exclude it from my for loop.

I might be wrong but your last code block might be correct if you just change it like below:

{% for image in page.media.images %}
   {% if image = profile_image_file %}
      <!-- do nothing, ignore that image -->
  {% endif %}
     <div data-src="{{ image.url }}" data-thumb=""></div>
{% endfor %}

Thanks for the suggestion, I just tried that but it results in an error.

Unexpected token “operator” of value “=” (“end of statement block” expected) in “partials/base.html.twig” at line 101.

apparently the combination of the if statement and = operator don’t match well.

oh, change image = profile_image_file for image == profile_image_file

Error is gone, but unfortunately it still shows the profile_image_file as well… tried clearing cache etc. without avail.

Will go over it once more… I’m pretty sure it should be a relatively easy solution…

I just tried the below, but also not working. Eventhough im 99% sure that for the image im targeting the image.urland page.media['profile_image_file'].url are identical.

{% if image.url == page.media['profile_image_file'].url %}
    <!--ignore this image -->
{% endif %}
---

Once again, I can’t test it right now but here is another thing you might try:

{% for image in page.media.images if image is not == page.header.profile_image_file %}
      <div data-src="{{ image.url }}" data-thumb=""></div>

{% endfor %}

Also, you can check if the two urls are identical by enabling the debugger in the configuration page and with the command dump()

Just got it working, by indeed checking the dump().

profile_image_file returned the filename only whereas image.url ofcourse returned the complete url.

image.filename works well, below the final if statement to exclude the image from a for loop in case someone stumbles across this as well.

{% if image.filename == profile_image_file %}
                                   <!-- ignore -->
{% else %}

Thanks for pointing me in the right direction!

good job!