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