1

I am using the Neo plugin in my project. I have made a Neofield article content with several block types in it. Amongst the block types that I have in it, is a gallery as well. Gallery block type has amatrix field imageWithCaption, and the field imageWithCaption has fields file which is the assets field, and fields imageTitle and caption which are plain text fields. I am able to display imageTitle and caption in my template but not a file, or the image itself. This is my template:

     {% for image in block.imageWithCaption %}
        <div class="large-4 columns">
          <div class="media-object stack-for-small">
            <div class="media-object-section">
              <div>
                <img src="{{ image.file }}" alt="">
              </div>
            </div>
            <div class="media-object-section">
              <h6>{{ image.title }}</h6>
              <p>{{ image.caption }}</p>
            </div>
          </div>
        </div>
      {% endfor %}

I get an error:

Object of class Craft\ElementCriteriaModel could not be converted to string

What I am doing wrong and how can I display the image?

3

It's tough to see the full context, just based on what you have here. From what I can tell, it seems like you might need to change <img src="{{ image.file }}" alt=""> to <img src="{{ image.file.first().url }}" alt="">.

Since the asset field can hold many assets, you have to declare .first() to grab the first one -- even if there's only one image. Then you have to grab the url property from the image, in order to get the correct src value.

|improve this answer|||||

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.