I have a hero-block.twig, which has the hero variable is set to fields.hero
{% set hero = fields.hero %}
{% for item in hero %}
{{item.title}}
{% endfor %}
I want to include the hero-block.twig to other twig field, with the hero is replaced with post.meta('hero'), but the hero doesn't return any value. Am I making a mistake somewhere?
{% include "block/hero-block.twig" with { hero : post.meta('hero') } %}
When you set
heroin hero-block.twig, you currently always set it tofields.hero. You should add the logic so thatherocan be overwritten.By using the null coalescing operator, you can check whether hero is passed in an include and use the passed
herovalue, or fall back tofields.hero.If you only use the variable once, you can also directly us that in the for loop.