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.
Anyone?
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?
lightswitch_field_handlewithentry.field_handleit should work as expected. – Victor Jul 14 '14 at 15:29