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 %}
    <body class="white">
{% 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 <body>-tags.

  • 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. – Brad Bell Jul 15 '14 at 19:37
  • Edited post, fixed the "-tag" display issue. – Lindsey D Jul 15 '14 at 20:06
  • Lol, @Lindsey. Must have saved my edit half a second after you did yours :) – carlcs Jul 15 '14 at 20:08
  • @Christian Photo finish! – Lindsey D Jul 15 '14 at 20:08

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 %}
    <body class="otherBodyClasses {{ bodyClass }}">
{% else %}
    <body class="otherBodyClasses">
{% endif %}

<main class="page" role="main">
    {% block content %}
    {% endblock %}


Or with the ternary syntax:

<body class="otherBodyClasses {{ bodyClass is defined ? bodyClass }}">



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:

<body class="otherBodyClasses {{ bodyClass is defined and != '' ? bodyClass }}">
| improve this answer | |
  • 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 Jul 15 '14 at 20:40
  • 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 Jul 15 '14 at 21:02
  • 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 Jul 15 '14 at 21:04
  • 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 Jul 15 '14 at 21:16
  • See the last example, this should work on your "projects" index.html. – carlcs Jul 15 '14 at 21:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.