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
    Nov 12 '15 at 18:59
  • Yes @BradBell {% set query = craft.request.getParam('q') %}
    – seth
    Nov 12 '15 at 19:20
  • What's block.body? A text field?
    – Brad Bell
    Nov 12 '15 at 19:22
  • block.body is rich text, block.heading is plain text.
    – seth
    Nov 12 '15 at 19:26
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, privacy policy and cookie policy

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