Answers.Body: <p>So I had missed the <code>entry.checkboxFieldHandle.contains('tequila')</code> line in the docs, which allows me to access the checkbox values outside the <code>for</code> loop.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-21T12:12:03.283Z
Answers.LastActivityDate: 2017-08-21T12:12:03.283Z
Answers.UserDisplayName: Sandwich
Body: <p>If I have a field of checkboxes, I'd like to be able to access them by the name of each checkbox as the variable name, and the 0 or 1 checked state as the value. Said variables need to be accessible beyond the <code>for</code> loop.</p>
  
  <p>For example (doesn't work):</p>
  
  <pre><code>{% for checkbox in checkboxes %}
      {% set {{ checkbox }} = checkbox.selected %}
  {% endfor %}
  </code></pre>
  
  <p>I've looked into things and seen answers mentioning <code>_context</code>, <code>attribute()</code>, and the like, but haven't been able to get anything to work.</p>
  
  <p>In short, if I have a checkboxes field with a single checkbox whose name is <code>my_checkbox</code>, label is "My Checkbox", and is checked in an entry, I'd like to be able to use <code>{{ my_checkbox }}</code> to output "1" (i.e. it's checked).</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-18T22:01:05.443Z
Comments.Text: You want to dynamically create Twig variables with the names based on selected labels in a checkbox field?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-18T23:41:06.130Z
Comments.Text: I believe so, yes. I _think_ in PHP it's called "variable variables"...?
Comments.UserDisplayName: Sandwich
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-18T23:56:54.550Z
Comments.Text: I can't image the use-case for this. :)  But I don't think it'll be possible from Twig alone.  Are you comfortable with PHP/Craft plugins?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-19T16:09:51.240Z
Comments.Text: The use case was to be able to refer to the checkbox value outside the `for` loop. I've since reread the [docs for Checkboxes](https://craftcms.com/docs/checkboxes-fields) and found `entry.checkboxFieldHandle.contains('tequila')`. >.<
Comments.UserDisplayName: Sandwich
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-20T17:30:11.093Z
Comments.Text: Ahh... would you mind adding what worked for you as an official answer?
Comments.UserDisplayName: Brad Bell
Tags: <variable>
Title: How can I dynamically set checkboxes to be named variables?
ThreadText: # How can I dynamically set checkboxes to be named variables?
  
  (Asked by: username_0 on 2017-08-18)
  
  If I have a field of checkboxes, I'd like to be able to access them by the name of each checkbox as the variable name, and the 0 or 1 checked state as the value. Said variables need to be accessible beyond the `for` loop.
  
  For example (doesn't work):
  
  ```
  {% for checkbox in checkboxes %}
      {% set {{ checkbox }} = checkbox.selected %}
  {% endfor %}
  
  ```
  
  I've looked into things and seen answers mentioning `_context`, `attribute()`, and the like, but haven't been able to get anything to work.
  
  In short, if I have a checkboxes field with a single checkbox whose name is `my_checkbox`, label is "My Checkbox", and is checked in an entry, I'd like to be able to use `{{ my_checkbox }}` to output "1" (i.e. it's checked).
  
  **Tags:** variable
  
  > **Comments:**
  > **username_1**: You want to dynamically create Twig variables with the names based on selected labels in a checkbox field?  
  > **username_0**: I believe so, yes. I _think_ in PHP it's called "variable variables"...?  
  > **username_1**: I can't image the use-case for this. :)  But I don't think it'll be possible from Twig alone.  Are you comfortable with PHP/Craft plugins?  
  > **username_0**: The use case was to be able to refer to the checkbox value outside the `for` loop. I've since reread the [docs for Checkboxes](https://craftcms.com/docs/checkboxes-fields) and found `entry.checkboxFieldHandle.contains('tequila')`. >.<  
  > **username_1**: Ahh... would you mind adding what worked for you as an official answer?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-08-21. Score: 2 | Accepted answer
  
  So I had missed the `entry.checkboxFieldHandle.contains('tequila')` line in the docs, which allows me to access the checkbox values outside the `for` loop.
  
  
  ---
  
  