<p>From your last update it looks like you simply need to get the entry that contains that matrix block, the error <code>Variable "entry" doesn't exist</code> simply means nothing is assigned to <code>entry</code> in your templates. There are a couple of ways you can solve this:</p>

<p><strong>Option 1</strong> - Tell Craft which template to load for this entry  </p>

<p>If you go into your section settings, there will be a field called <code>Entry template</code> which if, for example, you put <code>about</code> in this craft would look for either <code>about.html</code> or <code>about/index.html</code> in your <code>craft/templates</code> folder and would automatically assign a variable <code>entry</code> to your template when you visit the <code>/about</code> url.</p>

<p>There is also an option <code>This is for the homepage</code> in the settings which, if checked, will assign the entry to <code>index.html</code> in your templates (but i don't think this will show if there is already a section checked elsewhere)</p>

<p>You can see more about this in the craft docs: <a href="http://buildwithcraft.com/docs/templating-overview" rel="nofollow">http://buildwithcraft.com/docs/templating-overview</a></p>

<p><strong>Option 2</strong> - Fetch the entry in your template using twig tags</p>

<p>There are numerous ways to do this, but given you are using a single you should just be able to use this:</p>

<pre><code>{% set entry = craft.entries.section('homePageBanner').first() %}
</code></pre>

<p>You might need to adjust the <code>homePageBanner</code> part if that isn't the handle of your section, but this will then give you an <code>entry</code> variable to work with in your template and hopefully you'll find you will get the matrix content back :)</p>

<p>If you need some more details on how <code>craft.entries</code> works then the docs are a great place to find out more: <a href="http://buildwithcraft.com/docs/templating/craft.entries" rel="nofollow">http://buildwithcraft.com/docs/templating/craft.entries</a></p>

CC BY-SA 3.0
2015-08-20T22:40:03.737Z
I used the 1st option, and it works! Thanks a lot Alec, you rock! :) First obstacle is solved. Cant wait to dig deeper.
Sylvia Prabudy
CC BY-SA 3.0
2015-08-20T08:11:56.863Z
2015-08-20T08:11:56.863Z
Alec Ritson
<p>I would try this:</p>

<pre><code>{% for block in entry.bannerMatrix %}
  &lt;div&gt;
    {% set blockImage = block.image.first %}
    {% if blockImage %}
      &lt;img src="{{ blockImage.getUrl() }}" alt="{{ blockImage.filename }}"&gt;
    {% endif %}
    &lt;div class="hero_msg"&gt;
      &lt;h3&gt;{{ block.heading }}&lt;/h3&gt;
      &lt;h4&gt;{{ block.subheading }}&lt;/h4&gt;
    &lt;/div&gt;
  &lt;/div&gt;
{% endfor %}
</code></pre>

<ul>
<li>You loop over all the blocks :<code>{% for block in entry.bannerMatrix %}</code>.</li>
<li><code>block.image</code> will be an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, which is <strong>not</strong> an <a href="http://buildwithcraft.com/docs/templating/assetfilemodel" rel="nofollow">asset</a>, but a way to get an asset.</li>
<li>Set <code>blockImage</code> to the first (and only) asset if it exists, and otherwise <code>null</code>: <code>{% set blockImage = block.image.first %}</code>.</li>
<li>Only try to use the image if it exists: <code>{% if blockImage %}</code>.</li>
<li>Assets don't have a <code>title</code>, so <code>{{ blockImage.title }}</code> won't work. You can use <code>{{ blockImage.filename }}</code>, or you could assign another field in the asset's Field Layout, and then use that: <code>{{ blockImage.altText }}</code> for example.</li>
</ul>

CC BY-SA 3.0
2015-08-19T04:23:57.697Z
Marion, thanks for the answer. However, this blockImage dont show my entries either :( If you have any suggestions to change my matrix structures, I am open :)
Sylvia Prabudy
CC BY-SA 3.0
2015-08-19T16:45:46.870Z
Your matrix structures look fine. I would turn on `devMode` (in config/general.php), and add `{{ dump(blockImage) }}` to see what it is set to (right before the `if` line)
Marion Newlevant
CC BY-SA 3.0
2015-08-20T05:22:39.347Z
Hi Marion, when I turned on the devMode, it tells me that Variable "entry" does not exist (attached screenshots of the errors and the sections). Which part I didnt do it correctly?
Sylvia Prabudy
CC BY-SA 3.0
2015-08-20T18:50:14.493Z
Hi Sylvia, I think Alec has you covered here. Think about why you would expect `entry` to be set: either this is the entry template for some section, or you set it explicitly.
Marion Newlevant
CC BY-SA 3.0
2015-08-18T17:57:29.307Z
2015-08-18T17:57:29.307Z
Marion Newlevant
<p>thanks a lot for all the answers!</p>

<p>I finally got my entries to show up! What I did was move that entry to Homepage section(like what Alec Ritson suggested) together with other entry I already have in Homepage and wrapped the block code with
    {% for entry in craft.entries.section('homepage') %}</p>

<p>So the full code is:</p>

<pre><code>{% for entry in craft.entries.section('homepage') %}
    {% for block in entry.bannerMatrix %}
        &lt;div&gt;
            {% set blockImage = block.image.first() %}

            {% if blockImage %}
                &lt;img src="{{ blockImage.getUrl() }}" alt="{{ block.heading }}" title="{{ block.heading }}"&gt;
            {% endif %}

            &lt;div class="hero_msg"&gt;
                &lt;div class="caption_top"&gt;&lt;/div&gt;
                &lt;div class="caption"&gt;
                    &lt;h3&gt;{{ block.heading }}&lt;/h3&gt;

                    &lt;h4&gt;
                        &lt;span class="rsep"&gt;&lt;/span&gt;
                        {{ block.subheading }}
                        &lt;span class="lsep"&gt;&lt;/span&gt;
                    &lt;/h4&gt;

                &lt;/div&gt;
                &lt;div class="caption_bot"&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-08-20T22:33:32.253Z
2015-08-20T22:35:41.523Z
2015-08-20T22:35:41.523Z
Sylvia Prabudy
<p>Hi Sylvia and welcome to Craft. You are almost there with your markup, there are only 2 little things I can spot straight away:</p>

<p><strong>Line 4</strong> you are calling <code>{{ image.title }}</code> however the <code>image</code> variable doesn't exist anywhere from what I can see in your code.
In <strong>Line 1</strong> you are looping through your Matrix field with a <code>block</code> variable: <code>{% for block in ... %}</code>, so this means each time you reference a field you will need to start it with <code>{{ block. }}</code></p>

<p>If you change <strong>Line 4</strong> to <code>{{ block.image.first().title }}</code> then you will get what you are trying to achieve.</p>

<p>Also if your Matrix field only has one block type, there is no need to check that block type when looping through it. You can remove <code>{% if block.type == "banner" %}</code> and everything will still work like before.</p>

CC BY-SA 3.0
2015-08-17T06:58:11.373Z
2015-08-18T05:54:44.777Z
2015-08-18T05:54:44.777Z
Jamie Wade
<p>It's my first website using Craft.
I'm trying to figure out how to loop Matrix. I have created a field called <code>bannerMatrix</code> with a block type called <code>banner</code> that has 3 fields:</p>

<ol>
<li>Image (assets)</li>
<li>Heading (plain text)</li>
<li>Subheading (plain text)</li>
</ol>

<p>This is what I have so far:</p>

<pre><code>{% for block in entry.bannerMatrix %}
    {% if block.type == "banner" %}
        &lt;div&gt;
            &lt;img src="{{ block.image }}" alt="{{ image.title }}"&gt;
            &lt;div class="hero_msg"&gt;
                &lt;h3&gt;{{ block.heading }}&lt;/h3&gt;
                &lt;h4&gt;{{ block.subheading }}&lt;/h4&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Anything will be helpful.
Thanks!</p>

<hr>

<p><strong>UPDATE</strong></p>

<p>@JamieWade, thanks a lot for the super fast answer! 
Unfortunately, the code above still doesnt work for me :(</p>

<pre><code>{% for block in entry.bannerMatrix %}
    &lt;div&gt;
        &lt;img src="{{ block.image.first().title }}" alt=""&gt;
        &lt;div class="hero_msg"&gt;
            &lt;h3&gt;{{ block.heading }}&lt;/h3&gt;
            &lt;h4&gt;{{ block.subheading }}&lt;/h4&gt;
        &lt;/div&gt;
    &lt;/div&gt;
{% endfor %}
</code></pre>

<p>I also tried this :</p>

<pre><code>{% set image = block.image.first() %}
{% if image %}
    &lt;img src="{{ image.getUrl('image') }}"&gt;
{% endif %}
</code></pre>

<p>And I dont get anything as well :(</p>

<p>I wonder if my structure is correct. So I attached a screenshot.
<a href="https://i.sstatic.net/7Z2C9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7Z2C9.png" alt="enter image description here"></a></p>

<p>@Alec, yes there is content in the entry(I attached a screenshot). Now I'm even more curious, what's missing here. I also added the path to subfolder in the Field Setting.</p>

<p><a href="https://i.sstatic.net/t0Zr5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/t0Zr5.png" alt="enter image description here"></a></p>

<p><a href="https://i.sstatic.net/5oJUD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5oJUD.png" alt="enter image description here"></a>
<a href="https://i.sstatic.net/26Zfm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/26Zfm.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-08-18T10:37:04.233Z
The update looks pretty sound to me, unless i'm missing something too, It might seem obvious but are you sure there is content in the matrix block of the entry you are viewing?
Alec Ritson
CC BY-SA 3.0
2015-08-18T16:42:37.447Z
If you put an `{% else %}` just before the closing `{% endfor %}` and put some text between, do you see the text?
Alec Ritson
CC BY-SA 3.0
2015-08-19T04:09:20.703Z
Yes, I see the text! So looks like the code doesnt work for this case.
Sylvia Prabudy
CC BY-SA 3.0
2015-08-19T07:05:51.583Z
Silly I know but are you sure the "entry" variable is populated? Can you try a {{ dump(entry) }} above your code if you have devMode enabled?
Jérôme Coupé
CC BY-SA 3.0
2015-08-20T05:35:38.790Z
Jerome, I got Variable "entry" does not exist error :/ Not sure which part I didnt do it right. I have Matrix field called bannerMatrix, has 1 block with 3 fields (image, heading, subheading). A section (home-page-banner) that linked to the matrix field. And I put those entries there in home-page-banner.
Sylvia Prabudy
<matrix><blocks>
Templating Matrix Block
# Templating Matrix Block

(Asked by: username_0 on 2015-08-17)

It's my first website using Craft.
I'm trying to figure out how to loop Matrix. I have created a field called bannerMatrix with a block type called banner that has 3 fields:

Image (assets)
Heading (plain text)
Subheading (plain text)

This is what I have so far:
{% for block in entry.bannerMatrix %}
    {% if block.type == "banner" %}
        
            
            
                {{ block.heading }}
                {{ block.subheading }}
            
        
    {% endif %}
{% endfor %}

Anything will be helpful.
Thanks!

UPDATE
@JamieWade, thanks a lot for the super fast answer! 
Unfortunately, the code above still doesnt work for me :(
{% for block in entry.bannerMatrix %}
    
        
        
            {{ block.heading }}
            {{ block.subheading }}
        
    
{% endfor %}

I also tried this :
{% set image = block.image.first() %}
{% if image %}
    
{% endif %}

And I dont get anything as well :(
I wonder if my structure is correct. So I attached a screenshot.

@Alec, yes there is content in the entry(I attached a screenshot). Now I'm even more curious, what's missing here. I also added the path to subfolder in the Field Setting.

**Tags:** matrix, blocks

> **Comments:**
> **username_1**: The update looks pretty sound to me, unless i'm missing something too, It might seem obvious but are you sure there is content in the matrix block of the entry you are viewing?  
> **username_1**: If you put an `{% else %}` just before the closing `{% endfor %}` and put some text between, do you see the text?  
> **username_0**: Yes, I see the text! So looks like the code doesnt work for this case.  
> **username_2**: Silly I know but are you sure the "entry" variable is populated? Can you try a {{ dump(entry) }} above your code if you have devMode enabled?  
> **username_0**: Jerome, I got Variable "entry" does not exist error :/ Not sure which part I didnt do it right. I have Matrix field called bannerMatrix, has 1 block with 3 fields (image, heading, subheading). A section (home-page-banner) that linked to the matrix field. And I put those entries there in home-page-banner.  

---

## 4 answers

### Answer by username_1 on 2015-08-20. Score: 3

From your last update it looks like you simply need to get the entry that contains that matrix block, the error Variable "entry" doesn't exist simply means nothing is assigned to entry in your templates. There are a couple of ways you can solve this:
Option 1 - Tell Craft which template to load for this entry  
If you go into your section settings, there will be a field called Entry template which if, for example, you put about in this craft would look for either about.html or about/index.html in your craft/templates folder and would automatically assign a variable entry to your template when you visit the /about url.
There is also an option This is for the homepage in the settings which, if checked, will assign the entry to index.html in your templates (but i don't think this will show if there is already a section checked elsewhere)
You can see more about this in the craft docs: http://buildwithcraft.com/docs/templating-overview
Option 2 - Fetch the entry in your template using twig tags
There are numerous ways to do this, but given you are using a single you should just be able to use this:
{% set entry = craft.entries.section('homePageBanner').first() %}

You might need to adjust the homePageBanner part if that isn't the handle of your section, but this will then give you an entry variable to work with in your template and hopefully you'll find you will get the matrix content back :)
If you need some more details on how craft.entries works then the docs are a great place to find out more: http://buildwithcraft.com/docs/templating/craft.entries

> **Comments:**
> **username_0**: I used the 1st option, and it works! Thanks a lot Alec, you rock! :) First obstacle is solved. Cant wait to dig deeper.  

---

### Answer by username_3 on 2015-08-18. Score: 2

I would try this:
{% for block in entry.bannerMatrix %}
  
    {% set blockImage = block.image.first %}
    {% if blockImage %}
      
    {% endif %}
    
      {{ block.heading }}
      {{ block.subheading }}
    
  
{% endfor %}


You loop over all the blocks :{% for block in entry.bannerMatrix %}.
block.image will be an ElementCriteriaModel, which is not an asset, but a way to get an asset.
Set blockImage to the first (and only) asset if it exists, and otherwise null: {% set blockImage = block.image.first %}.
Only try to use the image if it exists: {% if blockImage %}.
Assets don't have a title, so {{ blockImage.title }} won't work. You can use {{ blockImage.filename }}, or you could assign another field in the asset's Field Layout, and then use that: {{ blockImage.altText }} for example.

> **Comments:**
> **username_0**: Marion, thanks for the answer. However, this blockImage dont show my entries either :( If you have any suggestions to change my matrix structures, I am open :)  
> **username_3**: Your matrix structures look fine. I would turn on `devMode` (in config/general.php), and add `{{ dump(blockImage) }}` to see what it is set to (right before the `if` line)  
> **username_0**: Hi Marion, when I turned on the devMode, it tells me that Variable "entry" does not exist (attached screenshots of the errors and the sections). Which part I didnt do it correctly?  
> **username_3**: Hi Sylvia, I think Alec has you covered here. Think about why you would expect `entry` to be set: either this is the entry template for some section, or you set it explicitly.  

---

### Answer by username_0 on 2015-08-20. Score: 2

thanks a lot for all the answers!
I finally got my entries to show up! What I did was move that entry to Homepage section(like what username_1 suggested) together with other entry I already have in Homepage and wrapped the block code with
    {% for entry in craft.entries.section('homepage') %}
So the full code is:
{% for entry in craft.entries.section('homepage') %}
    {% for block in entry.bannerMatrix %}
        
            {% set blockImage = block.image.first() %}

            {% if blockImage %}
                
            {% endif %}

            
                
                
                    {{ block.heading }}

                    
                        
                        {{ block.subheading }}
                        
                    

                
                
            
        
    {% endfor %}
{% endfor %}


---

### Answer by username_4 on 2015-08-17. Score: 2

Hi Sylvia and welcome to Craft. You are almost there with your markup, there are only 2 little things I can spot straight away:

**Line 4** you are calling `{{ image.title }}` however the `image` variable doesn't exist anywhere from what I can see in your code.
In **Line 1** you are looping through your Matrix field with a `block` variable: `{% for block in ... %}`, so this means each time you reference a field you will need to start it with `{{ block. }}`

If you change **Line 4** to `{{ block.image.first().title }}` then you will get what you are trying to achieve.

Also if your Matrix field only has one block type, there is no need to check that block type when looping through it. You can remove `{% if block.type == "banner" %}` and everything will still work like before.


---

