<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>

CC BY-SA 3.0
2017-08-21T12:12:03.283Z
2017-08-21T12:12:03.283Z
Sandwich
<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>

CC BY-SA 3.0
2017-08-18T22:01:05.443Z
You want to dynamically create Twig variables with the names based on selected labels in a checkbox field?
Brad Bell
CC BY-SA 3.0
2017-08-18T23:41:06.130Z
I believe so, yes. I _think_ in PHP it's called "variable variables"...?
Sandwich
CC BY-SA 3.0
2017-08-18T23:56:54.550Z
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?
Brad Bell
CC BY-SA 3.0
2017-08-19T16:09:51.240Z
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')`. >.<
Sandwich
CC BY-SA 3.0
2017-08-20T17:30:11.093Z
Ahh... would you mind adding what worked for you as an official answer?
Brad Bell
<variable>
How can I dynamically set checkboxes to be named variables?
# 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.


---

