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 :)

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 :)

1
  • 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

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.