3

I have a case where I need to access a previous matrix field's value (if it exists) in the next matrix loop result.

{% for contentBlock in entry.contentBlocks %}
    {% if contentBlock.prev %}
        {% if contentBlock.getPrev().matrixField | length %}
            {{ contentBlock.getPrev().matrixField }}
        {% endif %}
    {% endif 
    ....
{% endfor %}

I know that my issue is with {{ contentBlock.getPrev().matrixField }} but I am not sure how to access the field value after trying several methods. I'm sure it's something simple.

Thanks :)

Steve Adams
  • 1,671
  • 2
  • 15
  • 27

1 Answers1

3

As it turns out, you must use bracketed and not dotted syntax for matrix fields.

{% for contentBlock in entry.contentBlocks %}
    {% if contentBlock.prev %}
        {% if contentBlock.prev[matrixField] is defined %}
            {{ contentBlock.prev[matrixField] }}
        {% endif %}
    {% endif 
    ....
{% endfor %}

fixed this issue for me :)

Steve Adams
  • 1,671
  • 2
  • 15
  • 27
  • I had to enclose the field handle in quotes otherwise I got a "variable does not exist" erorr {{ contentBlock.prev['matrixField'] }} – Gogster Jan 25 '16 at 18:43