<p>It might seem counter-intuitive in cases where you only have 1 <em>block type</em> (i.e. <code>quote</code>) and only a single block, but your Matrix field will always be an <em>array</em>, where the blocks (i.e. the "children" of the Matrix array) aren't directly accessible:</p>

<pre><code>entry.quotation
    - block1 (quote)
    ...
</code></pre>

<p>Usually, you'll output your blocks by <em>looping</em> through the Matrix field, checking each block's type. I find that using a <code>{% switch %}</code> statement is often cleaner than an if/else:</p>

<pre><code>{% for block in entry.quotation %}

    {% switch block.type %}

         {% case 'quote' %}
         &lt;blockquote&gt;
             &lt;p&gt;{{ block.quoteText }}&lt;/p&gt;
             &lt;cite&gt;{{ block.quoteBy }}&lt;/cite&gt;
             &lt;a href="{{ block.quoteUrl }}"&gt;QuoteUrl&lt;a&gt;
         &lt;/blockquote&gt;

    {% endswitch %}

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

<p>You can, however, opt to only output blocks of a certain type. This is done by "filtering" the Matrix field using the <code>type</code> method, and removes the need for the <code>switch</code> or if/else statement. This is convenient in cases where you know you'll only have 1 block type:</p>

<pre><code>{% for quote in entry.quotation.type( 'quote' ) %}
    &lt;blockquote&gt;
         &lt;p&gt;{{ quote.quoteText }}&lt;/p&gt;
         &lt;cite&gt;{{ quote.quoteBy }}&lt;/cite&gt;
         &lt;a href="{{ quote.quoteUrl }}"&gt;QuoteUrl&lt;a&gt;
     &lt;/blockquote&gt;
{% endfor %}
</code></pre>

<p>If you know there's only 1 block, you can even remove the loop entirely by explicitly targeting the <code>first</code> block for your filtered block type. The actual amount of code will be about the same as the above example, though:</p>

<pre><code>{% set quote = entry.quotation.type( 'quote' ).first %}
{% if quote %}
    &lt;blockquote&gt;
        &lt;p&gt;{{ quote.quoteText }}&lt;/p&gt;
        &lt;cite&gt;{{ quote.quoteBy }}&lt;/cite&gt;
        &lt;a href="{{ quote.quoteUrl }}"&gt;QuoteUrl&lt;a&gt;
    &lt;/blockquote&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-04-27T04:48:40.213Z
I will use the {% switch %} technique in other areas of my build, thank you for that.Could you expand on your switch example and demo how to switch between text and image fields? (I have another area which has 2 Block Types: text, photo and within each is a field: text, photo. How I I have nested these is probably some of the reason I am having trouble.
Craftnoob
CC BY-SA 3.0
2015-04-27T07:38:26.930Z
@Craftnoob No problem, happy to help out. If you create a new question for your other Matrix issue w/ the 2 block types, I'll be happy to take a look :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-04-27T22:05:27.463Z
Thanks @mmikkel I have asked that question [link](http://craftcms.stackexchange.com/questions/9477/how-do-you-use-a-matrix-to-toggle-between-multiple-fields-from-multiple-blocks)
Craftnoob
CC BY-SA 3.0
2015-04-26T08:22:14.893Z
2015-04-26T08:29:52.643Z
2015-04-26T08:29:52.643Z
Mats Mikkel Rummelhoff
<p>Since matrix blocks support more than one block you generally want to loop through the blocks, then grab the fields from each block (even if there is only one block defined).</p>

<pre><code>{% for block in entry.quotation %}
    {% if block.type == 'quote' %}
        &lt;blockquote&gt;
            &lt;p&gt;{{ block.quoteText }}&lt;/p&gt;
            &lt;cite&gt;{{ block.quoteBy }}&lt;/cite&gt;
            &lt;a href="{{ block.quoteUrl }}"&gt;QuoteUrl&lt;a&gt;
        &lt;/blockquote&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Fyi... I'm using camel case in the example for all the field handles as it's the craft standard, but use whatever you use — just make sure it's actually the field 'handle' and not the field 'name'.</p>

CC BY-SA 3.0
2015-04-26T05:35:54.253Z
2015-04-26T05:35:54.253Z
Douglas McDonald
<p>How do you display select Fields from a Matrix Block Type when there are multiple Fields?</p>

<p>I have 1 Matrix Field with Handle ‘Quotation’</p>

<p>The configuration for this Matrix is as follows:</p>

<p>1 Block Type with handle ‘quote’</p>

<p>This Block Type has 3 plain text Fields:
‘Quotetext’
‘QuoteBy’
‘QuoteUrl’</p>

<p>I havent been able to get any output at all from this Matrix following the documentation and into following HTML structure:</p>

<pre><code>&lt;blockquote&gt;
    &lt;p&gt;Quotetext&lt;/p&gt;
    &lt;cite&gt;QuoteBy&lt;/cite&gt;
    &lt;a href="QuoteUrl"&gt;QuoteUrl&lt;a&gt;
&lt;/blockquote&gt;
</code></pre>

<p>Can anyone help me solve this issue?
Thank you</p>

<templating><matrix>
How do you display select Fields from a Matrix Block Type when there are multiple Fields?
# How do you display select Fields from a Matrix Block Type when there are multiple Fields?

(Asked by: username_0 on 2015-04-26)

How do you display select Fields from a Matrix Block Type when there are multiple Fields?
I have 1 Matrix Field with Handle ‘Quotation’
The configuration for this Matrix is as follows:
1 Block Type with handle ‘quote’
This Block Type has 3 plain text Fields:
‘Quotetext’
‘QuoteBy’
‘QuoteUrl’
I havent been able to get any output at all from this Matrix following the documentation and into following HTML structure:

    Quotetext
    QuoteBy
    QuoteUrl


Can anyone help me solve this issue?
Thank you

**Tags:** templating, matrix


---

## 2 answers

### Answer by username_1 on 2015-04-26. Score: 2 | Accepted answer

It might seem counter-intuitive in cases where you only have 1 block type (i.e. quote) and only a single block, but your Matrix field will always be an array, where the blocks (i.e. the "children" of the Matrix array) aren't directly accessible:
entry.quotation
    - block1 (quote)
    ...

Usually, you'll output your blocks by looping through the Matrix field, checking each block's type. I find that using a {% switch %} statement is often cleaner than an if/else:
{% for block in entry.quotation %}

    {% switch block.type %}

         {% case 'quote' %}
         
             {{ block.quoteText }}
             {{ block.quoteBy }}
             QuoteUrl
         

    {% endswitch %}

{% endfor %}

You can, however, opt to only output blocks of a certain type. This is done by "filtering" the Matrix field using the type method, and removes the need for the switch or if/else statement. This is convenient in cases where you know you'll only have 1 block type:
{% for quote in entry.quotation.type( 'quote' ) %}
    
         {{ quote.quoteText }}
         {{ quote.quoteBy }}
         QuoteUrl
     
{% endfor %}

If you know there's only 1 block, you can even remove the loop entirely by explicitly targeting the first block for your filtered block type. The actual amount of code will be about the same as the above example, though:
{% set quote = entry.quotation.type( 'quote' ).first %}
{% if quote %}
    
        {{ quote.quoteText }}
        {{ quote.quoteBy }}
        QuoteUrl
    
{% endif %}

> **Comments:**
> **username_0**: I will use the {% switch %} technique in other areas of my build, thank you for that.Could you expand on your switch example and demo how to switch between text and image fields? (I have another area which has 2 Block Types: text, photo and within each is a field: text, photo. How I I have nested these is probably some of the reason I am having trouble.  
> **username_1**: @username_0 No problem, happy to help out. If you create a new question for your other Matrix issue w/ the 2 block types, I'll be happy to take a look :)  
> **username_0**: Thanks @mmikkel I have asked that question [link](http://craftcms.stackexchange.com/questions/9477/how-do-you-use-a-matrix-to-toggle-between-multiple-fields-from-multiple-blocks)  

---

### Answer by username_2 on 2015-04-26. Score: 1

Since matrix blocks support more than one block you generally want to loop through the blocks, then grab the fields from each block (even if there is only one block defined).
{% for block in entry.quotation %}
    {% if block.type == 'quote' %}
        
            {{ block.quoteText }}
            {{ block.quoteBy }}
            QuoteUrl
        
    {% endif %}
{% endfor %}

Fyi... I'm using camel case in the example for all the field handles as it's the craft standard, but use whatever you use — just make sure it's actually the field 'handle' and not the field 'name'.


---

