0

I'm working on a basic portfolio site wherein each entry or "project" has a body Matrix field with various blocks for various types of images. I'd like to make an index of all projects showing one slideshow/carousel for each project containing all images within that project.

The problem is that I don't know how to query all images at once = All images, all blocks, in the order they appear in the entry.

Here's my specific code in use. It's almost there but due to the 0 in image[0] it's only pulling in the first image from each instance of each Matrix Block. For example, there are two instances of the block called imageHalf and it pulls the first image from each.

I don't understand how to avoid this integer....

{% for block in entry.body %}

{% switch block.type %}

{% case "imageFull" %}

{% set image = block.image.total() %}

    <a href="{{ entry.url }}"><img data-lazy="
        {{ block.image[0].getUrl() }}
    " /></a>

{% case "imageHalf" %}

    <a href="{{ entry.url }}"><img data-lazy="
        {{ block.image[0].getUrl() }}
    " /></a>

{% case "imageCouple" %}

    <a href="{{ entry.url }}"><img data-lazy="
        {{ block.image[0].getUrl() }}
    " /></a>

{% endswitch %}

{% endfor %}

2 Answers 2

1

I think you need to use a {% switch %} statement like the following:

{% switch matrixBlock.type %}

{% case "text" %}

    {{ matrixBlock.textField | markdown }}

{% case "image" %}

    {{ matrixBlock.image[0].getImg() }}

{% default %}

    <p>A font walks into a bar.</p>
    <p>The bartender says, “Hey, we don’t serve your type in here!”</p>
{% endswitch %}

You can read more here: http://buildwithcraft.com/docs/templating/switch

2
  • 1
    Right! This is actually what I was using (sorry I didn't add it before). To be more specific, I'm hung up on how it's only pulling the first image/asset and not the others. How do I avoid the [0] ? Commented Jun 1, 2015 at 4:36
  • @JeremyPaulBeasley Looks like Mmikkel beat me to it but you need to do something like {% for image in block.image %} <a href="{{ entry.url }}"> <img data-lazy="{{ image.getUrl() }}" /> </a> {% endfor %} Commented Jun 1, 2015 at 15:50
1

An Assets field always returns an instance of ElementCriteriaModel – essentially an array-like object – even if there's only 1 (or even 0) files in the field.

This is why things like matrixBlock.image[0] – or matrixBlock.image.first() – is needed to pull an actual asset, that you can call getUrl() and other related methods on.

To output all images in an Assets field, you need to loop over the field:

{% for image in block.image %}
    <a href="{{ entry.url }}">
        <img data-lazy="{{ image.getUrl() }}" />
    </a>
{% endfor %}

To put it all together, in this case it probably makes sense to filter the Matrix field using the type method, in order to only loop over the image blocks. This is especially useful if your Matrix block also contains other, non-image blocks – this way, you don't need the switch statement, and can get away with a minimal amount of code:

{% set imageBlocks = entry.body.type('imageFull, imageHalf, imageCouple') %}
{% if imageBlocks | length %}
    {% for block in imageBlocks %}
        {% for image in block.image %}
        <a href="{{ entry.url }}">
            <img data-lazy="{{ image.getUrl() }}" class="{{ block.type }}" />
        </a>
    {% endfor %}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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