Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I'm trying to use a Lightswitch field to insert a class on my body tag if the field is turned on.

What would be a good Twig template code to do this?

Edit 1:

Thanks for answering, guys. I still couldn't manage to do it, though. My lightswitch field handle is named whiteText. If I do this:

<body class='{% if whiteText('1') %}white{% endif %}'> 

it'll just say:

The function "whiteText" does not exist

(which I assume seems logical for you guys, but not for me).

And if I use entry.whiteText it'll say:

Craft\EntryModel and its behaviors do not have a method or closure named "whiteText".

I should also have some kind of "else" in the code, to output a clean -tag if the lightswitch is not switched on.

Edit 2:

I have now set up a global field. If I do {{ globalfieldhandle.lightswitchfieldhandle }} it outputs 0, even on the craft entry section I have it switched on (a single).

I need a way to make it say "1" if the lightswitch is turned on on the entry, and some help with the code to add a class on the body tag, and also the code for doing nothing if its not turned on.


The first answer to my question:

<body class='{% if lightswitch_field_handle %}class_name{% endif %}'>

seems like the right direction, but is it missing something?

share|improve this question
Edit 2: A global field is available globally, in every template, you can't set it per section. If you replace lightswitch_field_handle with entry.field_handle it should work as expected. –  Victor In Jul 14 at 15:29
Hi @Oyvind, and welcome to the Craft CMS Stack Exchange site. I deleted your previous answer because it wasn't actually an answer to the question. (We have to keep things tidy around here so future visitors can learn from your hard work.) You can edit your original question as much as you want—it's a good idea if you have more troubleshooting the share that results in a more specific question. –  Matt Stein Jul 14 at 16:26

1 Answer 1

Lightswitch fields save an integer to the database:

This integer can be checked just like a boolean, with Twigs if-tag:

<body class='{% if lightswitch_field_handle %}class_name{% endif %}'>

1 = true = on
0 = false = off


Twig has also a ternary operator which is similar to php's ternary operator:

<body class="{{ lightswitch_field_handle ? 'class_name' }}">

<body class="{{ lightswitch_field_handle ? 'class_name' : 'alt_class_name' }}">

Answer to Edit 1:

Your example doesn't work because you aren't comparing: if value == 1, you are checking a function: if function(param) == true. Check out the Twig if documentation for more examples.

If entry.whiteText doesn't work, your section doesn't have such a field. If you added it to your main template, you should check if the field is defined.

share|improve this answer
I'd also add the ternary style, Victor. {{ lightswitch_field_handle ? 'class_name' }} –  carlcs Jul 12 at 11:40
Also {{ lightswitch_field_handle ? 'class_name' : 'alt_class_name' }} –  carlcs Jul 12 at 11:42
Good point, thanks. Next time just edit it yourself ;) –  Victor In Jul 12 at 12:05
I've had success with: {% if entry.myLightswitch == "0" %}Lightswitch is off{% endif %} and {% if entry.myLightswitch == "1" %}Lightswitch is on{% endif %} –  Andrea DeMers Jul 16 at 13:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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