<p><a href="http://twig.sensiolabs.org/" rel="nofollow">Twig</a> is quite different from the EE parser – much more in line with how PHP is interpreted (Twig actually compiles to PHP before rendering).</p>

<p>First, the use of <code>gallery</code> in your <code>for</code> 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:</p>

<pre><code>{% for galleryEntry in block.galleryInclude %}
    {{ galleryEntry.title }}
    ...
{% endfor %}
</code></pre>

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

<pre><code>{% for slide in gallery.galleryMatrix %}
    ...
{% endfor %}
</code></pre>

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

<pre><code>{% for slide in gallery.galleryMatrix.type('slide') %}
    ...
{% endfor %}
</code></pre>

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

<pre><code>{% for image in slide.slideImage %}
    &lt;div&gt;&lt;img src="{{ image.getUrl() }}" /&gt;&lt;/div&gt;
{% endfor %}
</code></pre>

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

<pre><code>{% set slideImage = slide.slideImage.first() %}
{% if slideImage %}
    &lt;div&gt;&lt;img src="{{ asset.getUrl() }}" /&gt;&lt;/div&gt;
{% endif %}
</code></pre>

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

<pre><code>{% for gallery in block.galleryInclude %}
    {{ gallery.title }}

    &lt;figure&gt;
        {% for slide in gallery.galleryMatrix %}
            {% for image in slide.slideImage %}
                &lt;div&gt;&lt;img src="{{ image.getUrl() }}" /&gt;&lt;/div&gt;
            {% endfor %}
        {% endfor %}
   &lt;/figure&gt;

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-07T21:11:18.500Z
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
CC BY-SA 3.0
2015-05-07T21:13:58.747Z
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
CC BY-SA 3.0
2015-05-07T22:21:04.163Z
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
CC BY-SA 3.0
2015-05-07T22:22:49.730Z
```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
CC BY-SA 3.0
2015-05-07T20:28:42.573Z
2015-05-07T20:28:42.573Z
Mats Mikkel Rummelhoff
<p>Here are my channels and fields I need to get to work together.</p>

<p>Blog Channel (blog)</p>

<ol>
<li>Matrix Field (bodyMatrix)
<ul>
<li>Gallery Block (gallery)
<ul>
<li>Gallery Field (galleryInclude [entries])</li>
</ul></li>
</ul></li>
</ol>

<p>Gallery Channel (gallery)</p>

<ol start="2">
<li>Matrix Field (galleryMatrix)
<ul>
<li>Slide Block (slide)
<ul>
<li>Slide Image Field (slideImage [assets])</li>
<li>Slide Text Field (slideText)</li>
</ul></li>
</ul></li>
</ol>

<p>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.</p>

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

<p>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?</p>

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

<pre><code>{% for gallery in block.galleryInclude %}
  {{ gallery.title }}

  &lt;figure&gt;
    {% Gallery Matrix %}
      &lt;div&gt;&lt;img src={{gallery image url}}"&gt;&lt;/div&gt;
    {% end Gallery Matrix %}
  &lt;/figure&gt;
{% endfor %}
</code></pre>

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

<matrix><relations>
Getting Matrix content from within a relationship field
# Getting Matrix content from within a relationship field

(Asked by: username_0 on 2015-05-07)

Here are my channels and fields I need to get to work together.
Blog Channel (blog)

Matrix Field (bodyMatrix)

Gallery Block (gallery)

Gallery Field (galleryInclude [entries])



Gallery Channel (gallery)

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 }}

  
    {% Gallery Matrix %}
      
    {% end Gallery Matrix %}
  
{% endfor %}

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

**Tags:** matrix, relations

---

## 1 answers

### Answer by username_1 on 2015-05-07. Score: 2 | Accepted answer

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 %}
    
{% 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 %}
    
{% 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 }}

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

{% endfor %}

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  
> **username_0**: 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.  
> **username_1**: ```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 :)  

---

