1

Is there any possibility to use "entry" from the for-loop inside the "main" block inside another block ("header" in my case)? See example below:

{% extends "main" %}
    {% block header %}
        {{ entry.id }}
    {% endblock %}

{% block main %}
    {% for entry in craft.entries.section('blogPosts').limit(5).all() %}
    {% endfor %}
{% endblock %}

2 Answers 2

1

I don't understand what you want to achieve? In your example, the for loop would run 5 times so which entry.id do you want to get out?

The way to use common variables in both your blocks would be to do:

{% extends "main" %}
{% set entries = craft.entries.section('blogPosts').limit(5).all() %}

{% block header %}
    {% for entry in entries %}
        {# access entries in here #}
    {% endfor %}
{% endblock %}

{% block main %}
    {% for entry in entries %}
        {# and also in here #}
    {% endfor %}
{% endblock %}
1

The "entry" variable used inside the loop is restricted to the loop only.

You could create a variable earlier in the process, outside the blocks, like so:

{%- set posts = craft.entries.section('blogPosts').limit(5).all() -%}
{%- extends "main" -%}
{%- block header -%}
    {%- for post in posts -%}
        {{- post.id -}}
    {%- endfor -%}
{%- endblock -%}

{%- block main -%}
    {%- for post in posts -%}
    {%- endfor -%}
{%- endblock -%}

Then loop through it where you need to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy