Passing Matrix block to a macro I want to fetch a specific block from a Matrix global then pass it through a macro which returns a response based on the block type. However, if I try to use slice the type doesn't appear to be available to the macro. Has anyone experienced something similar?

eg if I set {% set timelineText = timeline.timeline %} (where first timeinline is my global and second timeline is my Matrix field) then fetch a specific block from the Matrix: {% set period = timelineText|slice(0,1) %} should return the first instance/block from the field. I can then pass this to my macro: {{ _self.doPeriod(period) }}

My macro is

{% macro doPeriod(content) %}
{% switch content.type %}
{% case 'quote' %}
    ...
{% case 'period' %}
    ...
{% endswitch %}    
{% endmacro %}```


But this returns the following error: Key "type" for array with keys "0" does not exist

Any thoughts?
First, you need to import your macro, and not just call {{ _self.doPeriod() }}. From the documentation:


  When you define a macro in the template where you are going to use it, you might be tempted to call the macro directly via _self.input() instead of importing it; even if seems to work, this is just a side-effect of the current implementation and it won't work anymore in Twig 2.x.


I always do: {% import _self as self %}

slice returns a sequence - in your case an array with one element, and the key is 0. Your macro is expecting a matrix block, not an array, which is why you get that error.

If you only want the first block, then {% set period = timelineText.first %} and period will be a matrix block. {{ self.doPeriod(period) }} will work.

If you want to call doPeriod on all of the matrix blocks, then:

{% for period in timelineText %}
  ... {{ self.doPeriod(period) }} ...
{% endfor %}