9

I'd like to output the Names of Matrix Blocks. I know you can filter by block types e.g.

{% for block in entry.myMatrixField.type('text, heading') %}
{% if block.type == "heading" %}
    <h3>{{ block.heading }}</h3>
{% else %}
    {{ block.text | markdown }}
{% endif %}
{% endfor %}

I know I could use {{ block.type }} and get the block type's handle but, I want to included the actual Name of the block type itself too.

For example, something like this:

{{ block.type.name }}

Is this currently possible?

13

block.type is actually an alias for a getType() function, which returns a new MatrixBlockTypeModel object. Those will output their handle by default, which is why it's possible to compare block.type against block type handles. But MatrixBlockTypeModel also has a handle property, so this is totally possible:

{{ block.type.handle }}
| improve this answer | |
  • Unfortunately, this means that using: {% if block.type in ['video', 'image', 'gallery'] %} …does not seem to work, as per this example. Is there another way? Using in is very tidy! – ianp Apr 28 '17 at 12:05
  • 1
    @ianp You need .name after block.type – Brandon Kelly Apr 29 '17 at 16:18
  • Thanks. I have tried that and no joy. Just to be clear, ( block.type == 'video' or block.type == 'image' or block.type == 'gallery' ) does still work, however. – ianp Apr 30 '17 at 17:00
  • 1
    Sorry I should have said .handle – Brandon Kelly Apr 30 '17 at 17:16
2

It does not appear to be in the matrix block model. http://buildwithcraft.com/docs/templating/matrixblockmodel

| improve this answer | |

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.