1

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:

<blockquote>
    <p>Quotetext</p>
    <cite>QuoteBy</cite>
    <a href="QuoteUrl">QuoteUrl<a>
</blockquote>

Can anyone help me solve this issue? Thank you

2

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' %}
         <blockquote>
             <p>{{ block.quoteText }}</p>
             <cite>{{ block.quoteBy }}</cite>
             <a href="{{ block.quoteUrl }}">QuoteUrl<a>
         </blockquote>

    {% 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' ) %}
    <blockquote>
         <p>{{ quote.quoteText }}</p>
         <cite>{{ quote.quoteBy }}</cite>
         <a href="{{ quote.quoteUrl }}">QuoteUrl<a>
     </blockquote>
{% 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 %}
    <blockquote>
        <p>{{ quote.quoteText }}</p>
        <cite>{{ quote.quoteBy }}</cite>
        <a href="{{ quote.quoteUrl }}">QuoteUrl<a>
    </blockquote>
{% endif %}
  • 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 Apr 27 '15 at 4:48
  • @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 Apr 27 '15 at 7:38
  • Thanks @mmikkel I have asked that question link – Craftnoob Apr 27 '15 at 22:05
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' %}
        <blockquote>
            <p>{{ block.quoteText }}</p>
            <cite>{{ block.quoteBy }}</cite>
            <a href="{{ block.quoteUrl }}">QuoteUrl<a>
        </blockquote>
    {% 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'.

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.