Getting item from List with specific title

I have a list field called “contentblocks” with two elements: A text field called “title” and an editor field called “content”.
So now in my Twig code I want to get the entry where the title is “youtube”. I tried like this:
{{ page.header.contentblocks[title=‘YouTube’].content}}

that does not work. What can I do?

Can you post the code for the .md file of the page that contains the list of contentblocks?

contentblocks:
    -
        title: '**What if your strings had the perfect partner?**'
        content: "my content"
    -
        title: '## Tempera ROSIN in a nutshell:'
        content: "my content 2"
 -
        title: 'YouTube'
        content: "my YouTube link"

List field is an array, so you should do an array search by value. Direct access would be something like:

{{ page.header.contentblocks[0].content}}
{{ page.header.contentblocks[1].content}}

This will print the content of the first matching element:

{{ page.header.contentblocks|filter(item => item.title == "YouTube")|first.content }}
2 Likes

You could use something like this:

{% for item in page.header.contentblocks %}
  {% if item.title == 'YouTube' %} 
     <strong>{{ item.title|raw }}</strong>
     < p>{{ item.content|raw }}</p>
  {% endif %}
{% endfor %} 

Within a loop it would still be better to use the filter IMHO:

{% for item in page.header.contentblocks|filter(cb => cb.title == "YouTube" -%}
     <strong>{{ item.title|raw }}</strong>
     <p>{{ item.content|raw }}</p>
{% endfor %} 

This will print all items that have a “YouTube” title

1 Like

thank you, this is exactly what I was looking for!