<p>To query on variable field names, you can use object notation; i.e. an object passed to <code>craft.entries()</code>:</p>
<pre><code>{% set field = 'genreGames' %}

{% set games = craft.entries({ (field): 'action' }).section('games').all() %}
</code></pre>
<p><em>Note the paranthesis around the <code>field</code> variable attribute; this is necessary to let Twig know that it should be parsed as a</em> variable, <em>and not interpreted literally as a field handle &quot;field&quot;.</em></p>
<p>It's possible to add multiple variable parameters (i.e. fields) using the object notation:</p>
<pre><code>{% set fieldA = 'genreGames' %}
{% set fieldB = 'somethingElse' %}

{% set games = craft.entries({ (fieldA): 'action', (fieldB): 'foo' }).section('games').all() %}
</code></pre>
<p>...but I'd recommend sticking with the common dot notation for any fields/parameters that doesn't have to be variables (i.e. <code>.section()</code>, in the above examples).</p>
<p>Object notation also works for relational fields (e.g. Entries fields), for example if you had an Entries field called <code>relatedGames</code> you could do this:</p>
<pre><code>{% set relatedGames = entry.relatedGames({ (field): 'action' }).all()
</code></pre>
<p>Aside from this, Jalen's suggestion for using <a href="https://twig.symfony.com/doc/3.x/tags/include.html" rel="nofollow noreferrer"><code>include</code></a> to create a re-usable &quot;query template&quot; partial (i.e. to act as a &quot;function&quot; to pass variables into and render the final template), is solid, and commonly the way situations like this are solved in Twig. You might also want to look into <a href="https://twig.symfony.com/doc/3.x/tags/macro.html" rel="nofollow noreferrer">Twig macros</a>, which are even more &quot;function-like&quot; than includes or embeds, but essentially these will just behave like a less flexible <code>include</code>.</p>

CC BY-SA 4.0
2022-11-16T18:09:52.833Z
Your solution is the best! But I don't understand how to correctly specify the path to the template in the construction {% include .... with {} %}
Dimi
CC BY-SA 4.0
2022-11-16T18:41:07.070Z
{% include 'filter/checkboxes.twig' with { .. } %} - This is my code that is written in index.twig (/templates/games/index.twig). File checkboxes.twig i put to the /templates/games/. It gives an error that it cannot find the template «filter/checkboxes.twig» in "games" at line 81
Dimi
CC BY-SA 4.0
2022-11-16T23:33:12.120Z
@Dimi Sounds like you just have the wrong path for the template you're trying to include. Twig includes are always relative to your `templates` folder, not to the template with the `{% include %}` tag. That means you always have to give it the full file path (albeit without `templates`). If the template you're trying to include exists at `templates/games/checkboxes.twig`, the include code should read `{% include 'games/checkboxes.twig' with { ... } %}`.  The code `{% include 'filter/checkboxes.twig' with { ... } %}` would look for a file `templates/filter/checkboxes.twig`; hence the error.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-16T00:04:55.893Z
2022-11-16T00:22:32.463Z
2022-11-16T00:22:32.463Z
Mats Mikkel Rummelhoff
<p>To answer your initial question, I <em>think</em> you could use the <a href="https://twig.symfony.com/doc/3.x/functions/attribute.html" rel="nofollow noreferrer"><code>attribute</code> function</a> here to solve your problem:</p>
<pre><code>{% set field = 'genreGames' %}
{{ attribute(games, field, 'action').all()|length }}
</code></pre>
<p>FYI, there is also the <a href="https://craftcms.com/docs/4.x/element-queries.html#count" rel="nofollow noreferrer"><code>.count()</code> method</a>, which could be a better fit here than <code>.all()|length</code> - works the same in this instance, but is shorter and more clear </p>
<hr />
<p>To answer your question/wish about the function thingy in Twig, I think what you might be looking for is something like this:</p>
<ol>
<li>Move your template code (i.e. what you highlighted as template code in your screenshot) into its own Twig file somewhere in your templates folder.</li>
<li>Then, you'll want to <a href="https://twig.symfony.com/doc/2.x/tags/include.html" rel="nofollow noreferrer">include</a> that template, and you can pass it your parameters (you don't even need the different <code>set</code>s you're currently doing):</li>
</ol>
<pre><code>{% include 'path/to/your/template/partial.twig' with {
    'name': 'genre',
    'type': 'checkbox',
    'field': 'genreGame',
    'fields': craft.app.fields.getFieldByHandle('genreGame')
} %}
</code></pre>
<p>There are some other things you could tinker with there, like the inclusion of <code>only</code> in the include function if you don't want the whole Twig context passed, or its possible an <a href="https://twig.symfony.com/doc/3.x/tags/embed.html" rel="nofollow noreferrer"><code>embed</code></a> might fit your use case more than the <code>include</code>.</p>
<hr />
<p>Hopefully that helps!</p>

CC BY-SA 4.0
2022-11-15T22:35:56.720Z
When i use function argument i get error: `craft\helpers\Template::attribute(): Argument #5 ($arguments) must be of type array, string given, called in /home/c/cf74574/mysite.com/storage/runtime/compiled_templates/0b/0b03a02d1223f07326227307d9f56f99afe2eb4c689d0c27fa6ccc336a931e9b.php on line 192`.  My code looks like this: `{{ attribute(games, 'genreGame', 'action').all()|length }}` varible games is array/ What i doing wrong?
Dimi
CC BY-SA 4.0
2022-11-15T23:13:12.597Z
PS. Just in case, I corrected the question and showed how I get the games variable
Dimi
CC BY-SA 4.0
2022-11-15T03:26:56.753Z
2022-11-15T06:06:24.210Z
2022-11-15T06:06:24.210Z
Jalen Davenport
<p>I'm trying to get the number of entries that have selected checkboxes.</p>
<p>This works successfully (where <code>genreGames</code> is a checkbox field):</p>
<pre><code>{{ games.genreGames('action').all()|length }}
</code></pre>
<p>Is there a way that I can do the same thing, except the field name is set dynamically? Like this:</p>
<pre><code>{% set games = craft.entries().section('games') %}
{% set field = 'genreGames' %}
{{ games.field('action').all()|length }}
</code></pre>
<p>Obviously that won't work, but I need something similar.</p>
<hr />
<p>The reason I need this is I'm building a solution for displaying the number of entries where a certain checkbox is checked. As you can see from the screenshot below, I want to set some parameters first and then use them below in my actual &quot;template&quot;. It would also be nice to create a function to pass these variables in to and then output the template, but I do not know if this is possible in Twig.</p>
<p><a href="https://i.sstatic.net/6bkfb.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6bkfb.jpg" alt="1" /></a></p>

CC BY-SA 4.0
2022-11-14T16:07:44.007Z
This is possible, but a bit involved. It's very likely there's a better solution. Why do you need to add a filter based on a variable field? Please include the context, then it's easier to suggest a better solution.
MoritzLost
CC BY-SA 4.0
2022-11-14T17:25:41.733Z
@MoritzLost, Made adjustments to the question
Dimi
<craft3>
How do I get the number of entries that have checkboxes?
# How do I get the number of entries that have checkboxes?

(Asked by: username_0 on 2022-11-14)

I'm trying to get the number of entries that have selected checkboxes.
This works successfully (where genreGames is a checkbox field):
{{ games.genreGames('action').all()|length }}

Is there a way that I can do the same thing, except the field name is set dynamically? Like this:
{% set games = craft.entries().section('games') %}
{% set field = 'genreGames' %}
{{ games.field('action').all()|length }}

Obviously that won't work, but I need something similar.

The reason I need this is I'm building a solution for displaying the number of entries where a certain checkbox is checked. As you can see from the screenshot below, I want to set some parameters first and then use them below in my actual "template". It would also be nice to create a function to pass these variables in to and then output the template, but I do not know if this is possible in Twig.

**Tags:** craft3
> **Comments:**
> **username_1**: This is possible, but a bit involved. It's very likely there's a better solution. Why do you need to add a filter based on a variable field? Please include the context, then it's easier to suggest a better solution.  
> **username_0**: @MoritzLost, Made adjustments to the question  

---

## 2 answers

### Answer by username_2 on 2022-11-16. Score: 2 | Accepted answer

To query on variable field names, you can use object notation; i.e. an object passed to craft.entries():
{% set field = 'genreGames' %}

{% set games = craft.entries({ (field): 'action' }).section('games').all() %}

Note the paranthesis around the field variable attribute; this is necessary to let Twig know that it should be parsed as a variable, and not interpreted literally as a field handle "field".
It's possible to add multiple variable parameters (i.e. fields) using the object notation:
{% set fieldA = 'genreGames' %}
{% set fieldB = 'somethingElse' %}

{% set games = craft.entries({ (fieldA): 'action', (fieldB): 'foo' }).section('games').all() %}

...but I'd recommend sticking with the common dot notation for any fields/parameters that doesn't have to be variables (i.e. .section(), in the above examples).
Object notation also works for relational fields (e.g. Entries fields), for example if you had an Entries field called relatedGames you could do this:
{% set relatedGames = entry.relatedGames({ (field): 'action' }).all()

Aside from this, Jalen's suggestion for using include to create a re-usable "query template" partial (i.e. to act as a "function" to pass variables into and render the final template), is solid, and commonly the way situations like this are solved in Twig. You might also want to look into Twig macros, which are even more "function-like" than includes or embeds, but essentially these will just behave like a less flexible include.

> **Comments:**
> **username_0**: Your solution is the best! But I don't understand how to correctly specify the path to the template in the construction {% include .... with {} %}  
> **username_0**: {% include 'filter/checkboxes.twig' with { .. } %} - This is my code that is written in index.twig (/templates/games/index.twig). File checkboxes.twig i put to the /templates/games/. It gives an error that it cannot find the template «filter/checkboxes.twig» in "games" at line 81  
> **username_2**: @Dimi Sounds like you just have the wrong path for the template you're trying to include. Twig includes are always relative to your `templates` folder, not to the template with the `{% include %}` tag. That means you always have to give it the full file path (albeit without `templates`). If the template you're trying to include exists at `templates/games/checkboxes.twig`, the include code should read `{% include 'games/checkboxes.twig' with { ... } %}`.  The code `{% include 'filter/checkboxes.twig' with { ... } %}` would look for a file `templates/filter/checkboxes.twig`; hence the error.  

---

### Answer by username_3 on 2022-11-15. Score: 2

To answer your initial question, I think you could use the attribute function here to solve your problem:
{% set field = 'genreGames' %}
{{ attribute(games, field, 'action').all()|length }}

FYI, there is also the .count() method, which could be a better fit here than .all()|length - works the same in this instance, but is shorter and more clear 

To answer your question/wish about the function thingy in Twig, I think what you might be looking for is something like this:

Move your template code (i.e. what you highlighted as template code in your screenshot) into its own Twig file somewhere in your templates folder.
Then, you'll want to include that template, and you can pass it your parameters (you don't even need the different sets you're currently doing):

{% include 'path/to/your/template/partial.twig' with {
    'name': 'genre',
    'type': 'checkbox',
    'field': 'genreGame',
    'fields': craft.app.fields.getFieldByHandle('genreGame')
} %}

There are some other things you could tinker with there, like the inclusion of only in the include function if you don't want the whole Twig context passed, or its possible an embed might fit your use case more than the include.

Hopefully that helps!

> **Comments:**
> **username_0**: When i use function argument i get error: `craft\helpers\Template::attribute(): Argument #5 ($arguments) must be of type array, string given, called in /home/c/cf74574/mysite.com/storage/runtime/compiled_templates/0b/0b03a02d1223f07326227307d9f56f99afe2eb4c689d0c27fa6ccc336a931e9b.php on line 192`.  My code looks like this: `{{ attribute(games, 'genreGame', 'action').all()|length }}` varible games is array/ What i doing wrong?  
> **username_0**: PS. Just in case, I corrected the question and showed how I get the games variable  

---

