Answers.Body: <p><a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow noreferrer">Lightswitch fields</a> save an <a href="http://php.net/manual/en/language.types.integer.php" rel="nofollow noreferrer">integer</a> to the database:</p>
  
  <p>This integer can be checked just like a <a href="http://php.net/manual/en/language.types.boolean.php" rel="nofollow noreferrer">boolean</a>, with <a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow noreferrer">Twigs if-tag</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;body class='{% if lightswitch_field_handle %}class_name{% endif %}'&gt;
  </code></pre>
  
  <blockquote>
    <p>1 = true  = on<br>
    0 = false = off</p>
  </blockquote>
  
  <p>.</p>
  
  <p>Twig has also a <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow noreferrer">ternary operator</a> which is similar to <a href="http://docs.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary" rel="nofollow noreferrer">php's ternary operator</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;body class="{{ lightswitch_field_handle ? 'class_name' }}"&gt;
  
  &lt;body class="{{ lightswitch_field_handle ? 'class_name' : 'alt_class_name' }}"&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Answer to Edit 1:</strong></p>
  
  <p>Your example doesn't work because you aren't comparing: <code>if value == 1</code>, you are checking a function: <code>if function(param) == true</code>. Check out the <a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow noreferrer">Twig if documentation</a> for more examples.</p>
  
  <p>If <code>entry.whiteText</code> doesn't work, your section doesn't have such a field. If you added it to your main template, you should <a href="https://craftcms.stackexchange.com/q/356/177">check if the field is defined</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T11:40:14.640Z
Answers.Text: I'd also add the ternary style, Victor. `{{ lightswitch_field_handle ? 'class_name' }}`
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T11:42:48.757Z
Answers.Text: Also `{{ lightswitch_field_handle ? 'class_name' : 'alt_class_name' }}`
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T12:05:06.880Z
Answers.Text: Good point, thanks. Next time just edit it yourself ;)
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-16T13:15:47.937Z
Answers.Text: I've had success with: {% if entry.myLightswitch == "0" %}Lightswitch is off{% endif %} and {% if entry.myLightswitch == "1" %}Lightswitch is on{% endif %}
Answers.UserDisplayName: Andrea DeMers
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T11:31:16.953Z
Answers.LastActivityDate: 2014-07-14T15:26:39.803Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Victor
Body: <p>I'm trying to use a <a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow">Lightswitch</a> field to insert a class on my body tag if the field is turned on. </p>
  
  <p>What would be a good Twig template code to do this?</p>
  
  <hr>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>Thanks for answering, guys. I still couldn't manage to do it, though. My lightswitch field handle is named whiteText. If I do this: </p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;body class='{% if whiteText('1') %}white{% endif %}'&gt; 
  </code></pre>
  
  <p>it'll just say: </p>
  
  <blockquote>
    <p>The function "whiteText" does not exist</p>
  </blockquote>
  
  <p>(which I assume seems logical for you guys, but not for me). </p>
  
  <p>And if I use <code>entry.whiteText</code> it'll say:</p>
  
  <blockquote>
    <p>Craft\EntryModel and its behaviors do not have a method or closure
    named "whiteText".</p>
  </blockquote>
  
  <p>I should also have some kind of "else" in the code, to output a clean -tag if the lightswitch is not switched on. </p>
  
  <hr>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>I have now set up a global field. If I do <code>{{ globalfieldhandle.lightswitchfieldhandle }}</code> it outputs 0, even on the craft entry section I have it switched on (a single). </p>
  
  <p>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.</p>
  
  <p>Anyone?</p>
  
  <p>The first answer to my question: </p>
  
  <pre><code>&lt;body class='{% if lightswitch_field_handle %}class_name{% endif %}'&gt;
  </code></pre>
  
  <p>seems like the right direction, but is it missing something?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-14T15:29:12.223Z
Comments.Text: 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.
Comments.UserDisplayName: Victor
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-14T16:26:32.343Z
Comments.Text: 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.
Comments.UserDisplayName: Matt Stein
Tags: <templating><lightswitch>
Title: Using a Lightswitch to insert a class on a body's tag
ThreadText: # Using a Lightswitch to insert a class on a body's tag
  
  (Asked by: username_0 on 2014-07-12)
  
  I'm trying to use a [Lightswitch](http://buildwithcraft.com/docs/lightswitch-fields) 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:
  
  ```twig
  <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?
  
  **Tags:** templating, lightswitch
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_2**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-12. Score: 6
  
  [Lightswitch fields](http://buildwithcraft.com/docs/lightswitch-fields) save an [integer](http://php.net/manual/en/language.types.integer.php) to the database:
  
  This integer can be checked just like a [boolean](http://php.net/manual/en/language.types.boolean.php), with [Twigs if-tag](http://twig.sensiolabs.org/doc/tags/if.html):
  
  ```twig
  <body class='{% if lightswitch_field_handle %}class_name{% endif %}'>
  
  ```
  
  > 1 = true = on  
  > 0 = false = off
  
  .
  
  Twig has also a [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators) which is similar to [php's ternary operator](http://docs.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary):
  
  ```twig
  <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](http://twig.sensiolabs.org/doc/tags/if.html) 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](https://craftcms.stackexchange.com/q/356/177).
  
  > **Comments:**
  > **username_3**: I'd also add the ternary style, username_1. `{{ lightswitch_field_handle ? 'class_name' }}`  
  > **username_3**: Also `{{ lightswitch_field_handle ? 'class_name' : 'alt_class_name' }}`  
  > **username_1**: Good point, thanks. Next time just edit it yourself ;)  
  > **username_4**: I've had success with: {% if entry.myLightswitch == "0" %}Lightswitch is off{% endif %} and {% if entry.myLightswitch == "1" %}Lightswitch is on{% endif %}  
  
  ---
  
  