<p>So, I had a similar problem... Had a few things that didn't work for me, but I got it to work. First item: putting the set bodyID inside the <code>{% block main %}</code> didn't work for me, it had to be outside of that. Second: it didn't like being a the switch/case format, so I did it as an if-check. </p>

<p>So, try something like this: </p>

<pre><code>{% extends '_layouts/main' %}

{% if entry.entryType.entryIs.value == "listing" %}
    {% set bodyId = 'listing' %}
{% elseif entry.entryType.entryIs.value == "listingSimple" %}
    {% set bodyId = 'listing-simple' %}
{% elseif entry.entryType.entryIs.value == "article" %}
    {% set bodyId = 'single-entry' %}
{% endif %}

{% block main %}

{% switch entry.entryType.entryIs.value %}
    {% case 'listing' %}
        {% include 'blog/_listing' %}
    {% case 'listingSimple' %}
        {% include 'blog/_listing-simple' %}
    {% case 'article' %}
        {% include 'blog/_single-entry' %}
{% endswitch %}

{% endblock %}
</code></pre>

CC BY-SA 4.0
2019-08-26T22:46:49.260Z
thanks that works. I could swear i tried that :-(
MrGlasspoole
CC BY-SA 4.0
2019-08-26T19:21:59.480Z
2019-08-26T19:21:59.480Z
SamC
<p>So Twig has a particular processing order for things like variables, includes, extends and all that sort of stuff. Basically, you can't set a variable in an included template and then use that in an extended template.</p>

<p>Here's a great article that goes into this topic in a little more depth: <a href="https://nystudio107.com/blog/twig-processing-order-and-scope" rel="nofollow noreferrer">https://nystudio107.com/blog/twig-processing-order-and-scope</a> - I'd highly recommend you give it a read.</p>

CC BY-SA 4.0
2019-08-26T18:53:01.297Z
2019-08-26T18:53:01.297Z
Jalen Davenport
<p>This is my _layouts/main.twig:</p>

<pre><code>&lt;body {% if bodyId is defined %}id="{{ bodyId }}"{% endif %}&gt;

  {% include '_layouts/header.twig' %}

  {% block main %}

  {% endblock %}

&lt;/body&gt;
</code></pre>

<p>Then i have blog/_entry.twig with:  </p>

<pre><code>{% extends '_layouts/main' %}

{% block main %}

{% switch entry.entryType.entryIs.value %}
    {% case 'listing' %}
        {% include 'blog/_listing.twig' %}
    {% case 'listingSimple' %}
        {% include 'blog/_listing-simple.twig' %}
    {% case 'article' %}
        {% include 'blog/_single-entry.twig' %}
{% endswitch %}

{% endblock %}
</code></pre>

<p>If I do:</p>

<pre><code>{% set bodyId = 'article' %}
</code></pre>

<p>in my blog/_single-entry.twig that does not work.  </p>

<p>But it works if i set bodyId in my index.twig in the root that is not a include.</p>

<craft3><include>
Setting body ID from includes does not work
# Setting body ID from includes does not work

(Asked by: username_0 on 2019-08-26)

This is my _layouts/main.twig:


  {% include '_layouts/header.twig' %}

  {% block main %}

  {% endblock %}



Then i have blog/_entry.twig with:  
{% extends '_layouts/main' %}

{% block main %}

{% switch entry.entryType.entryIs.value %}
    {% case 'listing' %}
        {% include 'blog/_listing.twig' %}
    {% case 'listingSimple' %}
        {% include 'blog/_listing-simple.twig' %}
    {% case 'article' %}
        {% include 'blog/_single-entry.twig' %}
{% endswitch %}

{% endblock %}

If I do:
{% set bodyId = 'article' %}

in my blog/_single-entry.twig that does not work.  
But it works if i set bodyId in my index.twig in the root that is not a include.

**Tags:** craft3, include


---

## 2 answers

### Answer by username_1 on 2019-08-26. Score: 2 | Accepted answer

So, I had a similar problem... Had a few things that didn't work for me, but I got it to work. First item: putting the set bodyID inside the {% block main %} didn't work for me, it had to be outside of that. Second: it didn't like being a the switch/case format, so I did it as an if-check. 
So, try something like this: 
{% extends '_layouts/main' %}

{% if entry.entryType.entryIs.value == "listing" %}
    {% set bodyId = 'listing' %}
{% elseif entry.entryType.entryIs.value == "listingSimple" %}
    {% set bodyId = 'listing-simple' %}
{% elseif entry.entryType.entryIs.value == "article" %}
    {% set bodyId = 'single-entry' %}
{% endif %}

{% block main %}

{% switch entry.entryType.entryIs.value %}
    {% case 'listing' %}
        {% include 'blog/_listing' %}
    {% case 'listingSimple' %}
        {% include 'blog/_listing-simple' %}
    {% case 'article' %}
        {% include 'blog/_single-entry' %}
{% endswitch %}

{% endblock %}

> **Comments:**
> **username_0**: thanks that works. I could swear i tried that :-(  

---

### Answer by username_2 on 2019-08-26. Score: 2

So Twig has a particular processing order for things like variables, includes, extends and all that sort of stuff. Basically, you can't set a variable in an included template and then use that in an extended template.

Here's a great article that goes into this topic in a little more depth: <https://nystudio107.com/blog/twig-processing-order-and-scope> - I'd highly recommend you give it a read.


---

