<p>I think there's a few ways you can do this with twig.</p>

<p>You should be able to check whether the category slug exists as an attribute on the entry object:</p>

<pre><code>{% for category in craft.categories.group('foodMenuCategories') %}
    {% if category.slug in entry.getAttributes|keys %} 
        ...
</code></pre>

<p>Or by using the category slug in an array accessor:</p>

<pre><code>{% for category in craft.categories.group('foodMenuCategories') %}
    {% if entry[category.slug] is defined %}
       ...
</code></pre>

<p>Or whether the getAttribute() method on the entry model returns anything truthy for that handle:</p>

<pre><code>{% for category in craft.categories.group('foodMenuCategories') %}
    {% if entry.getAttribute(category.slug) %} 
        ...
</code></pre>

<p>It's also important, that the slug is the same in every language!</p>

CC BY-SA 3.0
2014-09-17T13:43:06.310Z
the .handle is only for categoryGroups so I have to use something different - but the idea is perfect
maxx
CC BY-SA 3.0
2014-09-17T13:28:51.823Z
2014-09-17T14:14:14.213Z
2014-09-17T14:14:14.213Z
Mike Pepper
<p>I have a structure for different categories like salad, soup, main course and so on. Basically they share the same fields - but I want to be able to have dynamic singles based on those structures. For example I have a MainMenu-Single where I than can select entries from salads, soups, main courses and so on. </p>

<p>But is there a way to loop through all the fields in that single?</p>

<p>I tried:</p>

<pre><code>{% for category in craft.entries.section('foodMenuCategories') %}
    {% if entry.{category} is defined %} 
       ...
</code></pre>

<p>Sadly that doesn't work.</p>

CC BY-SA 3.0
2014-09-17T13:16:46.937Z
Not sure I am understanding the question so I don't want to answer outright, but you shouldn't have curly brackets within your twig. So `{% if entry.category is defined %}`
AbbeyDesign
CC BY-SA 3.0
2014-09-17T13:20:33.863Z
thats exacly my problem I have like ten different categories but they need exactly the same snippet and I want to automate the `{% if entry.soup is defined %}` `{% if entry.salad is defined %}` `{% if entry.mainCourse is defined %}` and so on
maxx
<entries><structure><singles>
Loop through fields from 'single'
# Loop through fields from 'single'

(Asked by: username_0 on 2014-09-17)

I have a structure for different categories like salad, soup, main course and so on. Basically they share the same fields - but I want to be able to have dynamic singles based on those structures. For example I have a MainMenu-Single where I than can select entries from salads, soups, main courses and so on. 
But is there a way to loop through all the fields in that single?
I tried:
{% for category in craft.entries.section('foodMenuCategories') %}
    {% if entry.{category} is defined %} 
       ...

Sadly that doesn't work.

**Tags:** entries, structure, singles
> **Comments:**
> **username_1**: Not sure I am understanding the question so I don't want to answer outright, but you shouldn't have curly brackets within your twig. So `{% if entry.category is defined %}`  
> **username_0**: thats exacly my problem I have like ten different categories but they need exactly the same snippet and I want to automate the `{% if entry.soup is defined %}` `{% if entry.salad is defined %}` `{% if entry.mainCourse is defined %}` and so on  

---

## 1 answers

### Answer by username_2 on 2014-09-17. Score: 3 | Accepted answer

I think there's a few ways you can do this with twig.
You should be able to check whether the category slug exists as an attribute on the entry object:
{% for category in craft.categories.group('foodMenuCategories') %}
    {% if category.slug in entry.getAttributes|keys %} 
        ...

Or by using the category slug in an array accessor:
{% for category in craft.categories.group('foodMenuCategories') %}
    {% if entry[category.slug] is defined %}
       ...

Or whether the getAttribute() method on the entry model returns anything truthy for that handle:
{% for category in craft.categories.group('foodMenuCategories') %}
    {% if entry.getAttribute(category.slug) %} 
        ...

It's also important, that the slug is the same in every language!

> **Comments:**
> **username_0**: the .handle is only for categoryGroups so I have to use something different - but the idea is perfect  

---

