1

supportContent is a Matrix field
contentBlock is a Matrix block
heading and body are fields within contentBlock

I am trying to display block.heading only if my query is in the block.body

{% for block in entry.supportContent.type('contentBlock') if query in block.body  %}
    <h3>{{ block.heading }}</h3>
{% endfor %}

This code is not working. Any help?

4
  • What's query? A string?
    – Brad Bell
    Commented Nov 12, 2015 at 18:59
  • Yes @BradBell {% set query = craft.request.getParam('q') %}
    – seth
    Commented Nov 12, 2015 at 19:20
  • What's block.body? A text field?
    – Brad Bell
    Commented Nov 12, 2015 at 19:22
  • block.body is rich text, block.heading is plain text.
    – seth
    Commented Nov 12, 2015 at 19:26

1 Answer 1

1

Your code would work as-is if block.body was a Plain Text field.

But since it's a Rich Text field, it technically returns a RichTextData object and Twig's in operator doesn't know what to do with objects.

This will grab the string data from the Rich Text field so in operates as you'd expect.

{% for block in entry.supportContent.type('contentBlock') if query in block.body.getRawContent()  %}
    <h3>{{ block.heading }}</h3>
{% endfor %}

Your Answer

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

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