<p>You can use the same lightswitch field on both of your channel sections.</p>

<p>But I wouldn't use an EntryModel in the <code>_layout.html</code> (assuming that's a template you use to extend your actual entry and index templates with).</p>

<p>You probably have two different <code>_entry.html</code> templates for your sections "project" and "events". Define a variable in these if your condition (lightswitch) is true:</p>



<pre class="lang-twig prettyprint-override"><code>{% extends "_layout" %}

{% if entry.whiteLogoText %}
    {% set bodyClass = 'white' %}
{% endif %}

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

<p>This is how your <code>_layout.html</code> should look like:</p>

<pre class="lang-twig prettyprint-override"><code>{% if bodyClass is defined %}
    &lt;body class="otherBodyClasses {{ bodyClass }}"&gt;
{% else %}
    &lt;body class="otherBodyClasses"&gt;
{% endif %}

&lt;main class="page" role="main"&gt;
    {% block content %}
    {% endblock %}
&lt;/main&gt;

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

<p>Or with the ternary syntax:</p>

<pre class="lang-twig prettyprint-override"><code>&lt;body class="otherBodyClasses {{ bodyClass is defined ? bodyClass }}"&gt;
</code></pre>

<p>.</p>

<p><strong><em>Edit:</em></strong></p>

<p>You can additionally set that <code>bodyClass</code> variable in other templates using different conditionals:</p>

<pre class="lang-twig prettyprint-override"><code>{% if myGlobalSetHandle.whiteLogoTextForProjectsEntries %}
    {% set bodyClass = 'white' %}
{% endif %}
</code></pre>

<p>or (based on my latest assumptions on what you wanna do):</p>

<pre class="lang-twig prettyprint-override"><code>{# Define var before the for loop #}
{% set bodyClass = '' %}

{# Check if `whiteLogoText` is set on any "project" entry #}
{% for project in craft.entries.section('projects') %}
    {% if project.whiteLogoText %}
        {% set bodyClass = 'white' %}
    {% endif %}
{% endfor %}
</code></pre>

<p>With this last code example you have to define the variable before the loop to make it accessible outside of it. Because you will always have a <code>bodyClass</code> defined then, change the conditional in your <code>_layout.html</code> like this:</p>

<pre class="lang-twig prettyprint-override"><code>&lt;body class="otherBodyClasses {{ bodyClass is defined and != '' ? bodyClass }}"&gt;
</code></pre>

CC BY-SA 3.0
2014-07-15T20:40:07.903Z
Thanks alot for helping out Christian. Its a bit more complicated: On the 'events' section "landing page" (index.html) this works fine. But on the 'projects' section I use a feed of entries to print the projects. I would like to define the lightswitch field on an unique project entry, and then output it with a feed of entries. Does this make sense?
osh
CC BY-SA 3.0
2014-07-15T21:02:31.457Z
Hmmm.. don't know if I get you right. Basically you want one lightswitch field, that is responsible for all of your `_entry.html` templates for that "projects" section?
carlcs
CC BY-SA 3.0
2014-07-15T21:04:56.823Z
Other option: you could have that field on each of you "projects" entries in the CP and then check in your template if it is set to true on any of these?
carlcs
CC BY-SA 3.0
2014-07-15T21:16:53.960Z
I have a landing page for 'events' (index.html), which is a single section. Telling this to get body class=white works fine, with the lightswitch field added to its field layout. I also have a landing page for 'projects' (index.html). This page (a single) feeds a bunch of projects (a channel). If its turned on in one of the entries in the feed, it should do body class=white. Just to make clear: None of this happens on _entry level, both instances are index.html. The body class=white class makes my menus links turn white, rather than black, if that makes any sense. Thanks for your patience!
osh
CC BY-SA 3.0
2014-07-15T21:20:18.030Z
See the last example, this should work on your "projects" `index.html`.
carlcs
CC BY-SA 3.0
2014-07-15T21:30:14.063Z
Corrected a code error that printed 'bodyClass' instead of 'white' (the value of `bodyClass`)!
carlcs
CC BY-SA 3.0
2014-07-15T21:37:40.567Z
@Oyvind could you please copy some of that additional info you provided to your question text?
carlcs
CC BY-SA 3.0
2014-07-16T11:24:55.053Z
@Oyvind it'd nice to hear if this ended up working for you!
carlcs
CC BY-SA 3.0
2014-08-27T14:12:24.963Z
@osh nice to see you're back and I hope everything's good so far. Could you please do me a favor and go through your Q/As and respond to open questions / accept helpful answers? Thanks!
carlcs
CC BY-SA 3.0
2014-07-15T20:01:35.507Z
2014-07-15T21:28:45.350Z
2014-07-15T21:28:45.350Z
carlcs
<p>I'm using a lightswitch field to add a class to my html document body-tag. The field is added to a section  with a handle named "events". </p>

<p>To make this happen, I do this in my _layout.html:</p>

<pre><code>{% if entry.whiteLogoText %}
    &lt;body class="white"&gt;
{% else %}
    &lt;body&gt;
{% endif %}
</code></pre>

<p>Now I also want to use such a lightswitch for entries of a different section (with a field handle named 'projects').</p>

<p>Should I make a new lightswitch field for this particular section, and if so, how should I print it?</p>

<p>I wanna make sure I don't get two <code>&lt;body&gt;</code>-tags.</p>

CC BY-SA 3.0
2014-07-15T19:37:27.880Z
Can you clarify/add some context to this?  You mention a handle named events, but I don't see that referenced in your template code. and I'm not sure what "-tag"  is you're referring to.
Brad Bell
CC BY-SA 3.0
2014-07-15T20:06:55.617Z
Edited post, fixed the "-tag" display issue.
Lindsey D
CC BY-SA 3.0
2014-07-15T20:08:18.453Z
Lol, @Lindsey. Must have saved my edit half a second after you did yours :)
carlcs
CC BY-SA 3.0
2014-07-15T20:08:57.810Z
@Christian Photo finish!
Lindsey D
<conditional><lightswitch>
Two different lightswitches to add class on body tag
# Two different lightswitches to add class on body tag

(Asked by: username_0 on 2014-07-15)

I'm using a lightswitch field to add a class to my html document body-tag. The field is added to a section  with a handle named "events". 
To make this happen, I do this in my _layout.html:
{% if entry.whiteLogoText %}
    
{% else %}
    
{% endif %}

Now I also want to use such a lightswitch for entries of a different section (with a field handle named 'projects').
Should I make a new lightswitch field for this particular section, and if so, how should I print it?
I wanna make sure I don't get two -tags.

**Tags:** conditional, lightswitch

> **Comments:**
> **username_1**: Can you clarify/add some context to this?  You mention a handle named events, but I don't see that referenced in your template code. and I'm not sure what "-tag"  is you're referring to.  
> **username_2**: Edited post, fixed the "-tag" display issue.  
> **username_3**: Lol, @Lindsey. Must have saved my edit half a second after you did yours :)  
> **username_2**: @Christian Photo finish!  

---

## 1 answers

### Answer by username_3 on 2014-07-15. Score: 1

You can use the same lightswitch field on both of your channel sections.
But I wouldn't use an EntryModel in the _layout.html (assuming that's a template you use to extend your actual entry and index templates with).
You probably have two different _entry.html templates for your sections "project" and "events". Define a variable in these if your condition (lightswitch) is true:
{% extends "_layout" %}

{% if entry.whiteLogoText %}
    {% set bodyClass = 'white' %}
{% endif %}

{% block content %}
{% endblock %}

This is how your _layout.html should look like:
{% if bodyClass is defined %}
    
{% else %}
    
{% endif %}


    {% block content %}
    {% endblock %}




Or with the ternary syntax:


.
Edit:
You can additionally set that bodyClass variable in other templates using different conditionals:
{% if myGlobalSetHandle.whiteLogoTextForProjectsEntries %}
    {% set bodyClass = 'white' %}
{% endif %}

or (based on my latest assumptions on what you wanna do):
{# Define var before the for loop #}
{% set bodyClass = '' %}

{# Check if `whiteLogoText` is set on any "project" entry #}
{% for project in craft.entries.section('projects') %}
    {% if project.whiteLogoText %}
        {% set bodyClass = 'white' %}
    {% endif %}
{% endfor %}

With this last code example you have to define the variable before the loop to make it accessible outside of it. Because you will always have a bodyClass defined then, change the conditional in your _layout.html like this:

> **Comments:**
> **username_0**: Thanks alot for helping out Christian. Its a bit more complicated: On the 'events' section "landing page" (index.html) this works fine. But on the 'projects' section I use a feed of entries to print the projects. I would like to define the lightswitch field on an unique project entry, and then output it with a feed of entries. Does this make sense?  
> **username_3**: Hmmm.. don't know if I get you right. Basically you want one lightswitch field, that is responsible for all of your `_entry.html` templates for that "projects" section?  
> **username_3**: Other option: you could have that field on each of you "projects" entries in the CP and then check in your template if it is set to true on any of these?  
> **username_0**: I have a landing page for 'events' (index.html), which is a single section. Telling this to get body class=white works fine, with the lightswitch field added to its field layout. I also have a landing page for 'projects' (index.html). This page (a single) feeds a bunch of projects (a channel). If its turned on in one of the entries in the feed, it should do body class=white. Just to make clear: None of this happens on _entry level, both instances are index.html. The body class=white class makes my menus links turn white, rather than black, if that makes any sense. Thanks for your patience!  
> **username_3**: See the last example, this should work on your "projects" `index.html`.  
> **username_3**: Corrected a code error that printed 'bodyClass' instead of 'white' (the value of `bodyClass`)!  
> **username_3**: @Oyvind could you please copy some of that additional info you provided to your question text?  
> **username_3**: @Oyvind it'd nice to hear if this ended up working for you!  
> **username_3**: @username_0 nice to see you're back and I hope everything's good so far. Could you please do me a favor and go through your Q/As and respond to open questions / accept helpful answers? Thanks!  

---

