Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T13:43:06.310Z
Answers.Text: the .handle is only for categoryGroups so I have to use something different - but the idea is perfect
Answers.UserDisplayName: maxx
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T13:28:51.823Z
Answers.LastActivityDate: 2014-09-17T14:14:14.213Z
Answers.LastEditDate: 2014-09-17T14:14:14.213Z
Answers.UserDisplayName: Mike Pepper
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T13:16:46.937Z
Comments.Text: 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 %}`
Comments.UserDisplayName: AbbeyDesign
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-17T13:20:33.863Z
Comments.Text: 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
Comments.UserDisplayName: maxx
Tags: <entries><structure><singles>
Title: Loop through fields from 'single'
ThreadText: # 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  
  
  ---
  
  