1

Here are my channels and fields I need to get to work together.

Blog Channel (blog)

  1. Matrix Field (bodyMatrix)
    • Gallery Block (gallery)
      • Gallery Field (galleryInclude [entries])

Gallery Channel (gallery)

  1. Matrix Field (galleryMatrix)
    • Slide Block (slide)
      • Slide Image Field (slideImage [assets])
      • Slide Text Field (slideText)

In my Blog Channel, I have a Gallery block inside a Body Matrix field. This Gallery block contains just an Entries relationship field that is pulling from my Gallery channel.

Inside the Gallery channel is a Matrix field as well, with a single block for slideImage and slideText.

This being my second day working with Craft, but years of experience with EE, how do I pull in the content from that Gallery entry and it's Gallery Matrix?

I've been able to pull in that Gallery title, but can't at all get the Gallery Matrix to output anything.

{% for gallery in block.galleryInclude %}
  {{ gallery.title }}

  <figure>
    {% Gallery Matrix %}
      <div><img src={{gallery image url}}"></div>
    {% end Gallery Matrix %}
  </figure>
{% endfor %}

I'm not even sure when I use 'gallery' above, if that's the right variable to use, but it works.

2

Twig is quite different from the EE parser – much more in line with how PHP is interpreted (Twig actually compiles to PHP before rendering).

First, the use of gallery in your for loop is arbitrary – its just the name you pick for the variable containing the currently iterated item, and its completely up to you. So this would work just as well:

{% for galleryEntry in block.galleryInclude %}
    {{ galleryEntry.title }}
    ...
{% endfor %}

Second, there are a couple of issues with your code. For one, you'll need to use another for loop to access the slides inside the galleryMatrix field. Note that you can use "block" as the variable name here as well (unless you need to access any of the outer block variable's properties), but let's call it "slide" for clarity:

{% for slide in gallery.galleryMatrix %}
    ...
{% endfor %}

Note that if your galleryMatrix field contains multiple block types and you're only interested in the "slide" block type, you can for test the slide.type property inside the loop, or use the type method to filter the galleryMatrix field:

{% for slide in gallery.galleryMatrix.type('slide') %}
    ...
{% endfor %}

Furthermore – this is a common gotcha for new Craft users – even if there's only 1 asset, you'll always need to loop through any Asset field; you can't just put assetField.getUrl(). So, you'll need another loop to pull your slide image(s) inside the galleryMatrix loop:

{% for image in slide.slideImage %}
    <div><img src="{{ image.getUrl() }}" /></div>
{% endfor %}

Sidenote; when there's only a single asset you could also do the following, but as you'll need an if statement the amount of code won't be any less:

{% set slideImage = slide.slideImage.first() %}
{% if slideImage %}
    <div><img src="{{ asset.getUrl() }}" /></div>
{% endif %}

Putting it together, here's what your full code should look like (not tested, but should work based on the info you gave):

{% for gallery in block.galleryInclude %}
    {{ gallery.title }}

    <figure>
        {% for slide in gallery.galleryMatrix %}
            {% for image in slide.slideImage %}
                <div><img src="{{ image.getUrl() }}" /></div>
            {% endfor %}
        {% endfor %}
   </figure>

{% endfor %}
  • Thank you mmikkel. I'm assuming that 'gallery', 'slide', and 'image' are variables that you named yourself? Nothing significant about them other than they describe the content they are dealing with? – John Johnson May 7 '15 at 21:11
  • Yes, the first parameter of the for loops are variable names which can be whatever you want. Nothing more to it than semantics. Personally, I'm always partial to a super specific, if slightly longer variable names, over generic short ones. As an example, I'd probably go with galleryEntry over gallery. But yeah, totally up to you. – Mats Mikkel Rummelhoff May 7 '15 at 21:13
  • mmikkel, one last question: How do I get the URL of the related entry? {{ entry.url }} inside the galleryInclude block grabbed the current page's URL instead. – John Johnson May 7 '15 at 22:21
  • entry refers to the entry you're currently viewing (its a variable pre-populated by Craft). Your related entry is stored in the variable gallery, so {{ gallery.url }} should do it :) – Mats Mikkel Rummelhoff May 7 '15 at 22:22

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.