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?

0

2 Answers 2

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 }}
4
  • 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, 2017 at 12:05
  • 1
    @ianp You need .name after block.type Apr 29, 2017 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, 2017 at 17:00
  • 1
    Sorry I should have said .handle Apr 30, 2017 at 17:16
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.