1

I'm stuck with the .first() element and why the for-loop code won't work.

<div id="header-slider">
  <ul>        
    {% set image = entry.bannerSlider.first() %}
    {% set transformedImage = craft.imager.transformImage(image, 
      { width: 1420, height: 550, position: image.focalPoint,  effects: { grayscale: true, gamma: 1.5}, }) %}
    {% for image in entry.bannerSlider %}
      <li data-transition="" data-masterspeed="500">
        <img src="{{ transformedImage.url }}"/>
      </li>
    {% endfor %}
  </ul>
</div>

If I remove the .first() element the code breaks ("An unknown image object was used"), but, as this code populates a slider, I need all the images loaded, hence my attempt with the for-loop.

I think I am mixing up different Models, but need someone to point it out, please...

  • Going on that error, you are mixing the models up indeed. What is bannerSlider, Is it a asset field, matrix or something else? – Fyrebase Jan 9 '17 at 3:46
  • The bannerSlider is an asset field. – Nutmeg Jan 9 '17 at 3:57
4

When using the for loop you don't need to set image as it is set in the for loop. You will also need to define transformedImage on every iteration, so move it within the for loop.

Give the below a go, it should do what you are after.

<div id="header-slider">
  <ul>
    {% for image in entry.bannerSlider %}
      {% set transformedImage = craft.imager.transformImage(image, { width: 1420, height: 550, position: image.focalPoint,  effects: { grayscale: true, gamma: 1.5}, }) %}
      <li data-transition="" data-masterspeed="500">
        <img src="{{ transformedImage.url }}"/>
      </li>
    {% endfor %}
  </ul>
</div>
  • What if you wanted to pull the image out of another section? Like {% for image in craft.entries.section('news').limit(1) %} {% set transformedImage = craft.imager.transformImage(image, { width: 1420, height: 550, position: image.focalPoint }) %} <img src="{{ image.bannerImage.first().getUrl }}" alt="" /> {% endfor %} Where do you then define the image? (bannerImage = asset field) – Nutmeg Jan 9 '17 at 4:42
  • This would be better as a new question. But if you wanted to pull images from different sections you would be better off building up an array in twig then looping through that array to display the images – Fyrebase Jan 9 '17 at 6:44
  • yes, I agree - would you mind following this up in this new thread? – Nutmeg Jan 9 '17 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.