Answers.Body: <p>You can use the <a href="https://docs.craftcms.com/v3/dev/functions.html#classname-object" rel="noreferrer">className</a> function</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% switch className(field) %}
      {% case 'craft\\fields\\PlainText' %}
      {% case 'craft\\fields\\Dropdown' %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-15T14:19:53.377Z
Answers.LastActivityDate: 2018-08-15T14:19:53.377Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I would like to loop through all the fields of a field layout tab form the current entry.</p>
  
  <p>This is working fine to get the field name and value:</p>
  
  <pre><code>{% for fieldLayoutField in entry.getFieldLayout().getFields() if fieldLayoutField.tabId == 50 %}
      {% set field = craft.app.fields.getFieldById(fieldLayoutField.id) %}
      Field name: {{ field.name }}
      Field value: entry[field.handle]
  {% endfor %}
  </code></pre>
  
  <p>But I need to go a step further, by checking the field type, and then handle something different depending of the type.</p>
  
  <p>Example:</p>
  
  <pre><code>{% if field.type == 'dropdown' %}
      &lt;select&gt;
          {% for option in field.options %}
              &lt;option value="{{ option.value }}"&gt;
                  {{ option.label }}
              &lt;/option&gt;
          {% endfor %}
      &lt;/select&gt;
  {% endif %}
  </code></pre>
  
  <p>As <code>{{ field.type }}</code> and <code>{{ entry[field.handle].type }}</code> aren't defined, I can't guess properly when this is, for example, a Plain Text field or a Dropdown field.</p>
  
  <p>I could guess some fields type, by some checks like bellow, but it sounds very dirty:</p>
  
  <ul>
  <li><code>field.columnType</code>
  returns <code>text</code> if a Plain Text field.</li>
  <li><code>field.options | length</code> returns <code>true</code> if a Dropdown field.</li>
  </ul>
  
  <p>For some others, like the Lightswitch field, I don't know exactly how to guess the type properly.</p>
  
Tags: <templating><field><fieldtypes>
Title: How can I get the type of a field using Twig?
ThreadText: # How can I get the type of a field using Twig?
  
  (Asked by: username_0 on 2018-08-15)
  
  I would like to loop through all the fields of a field layout tab form the current entry.
  
  This is working fine to get the field name and value:
  
  ```
  {% for fieldLayoutField in entry.getFieldLayout().getFields() if fieldLayoutField.tabId == 50 %}
      {% set field = craft.app.fields.getFieldById(fieldLayoutField.id) %}
      Field name: {{ field.name }}
      Field value: entry[field.handle]
  {% endfor %}
  
  ```
  
  But I need to go a step further, by checking the field type, and then handle something different depending of the type.
  
  Example:
  
  ```
  {% if field.type == 'dropdown' %}
      <select>
          {% for option in field.options %}
              <option value="{{ option.value }}">
                  {{ option.label }}
              </option>
          {% endfor %}
      </select>
  {% endif %}
  
  ```
  
  As `{{ field.type }}` and `{{ entry[field.handle].type }}` aren't defined, I can't guess properly when this is, for example, a Plain Text field or a Dropdown field.
  
  I could guess some fields type, by some checks like bellow, but it sounds very dirty:
  
  * `field.columnType`
    returns `text` if a Plain Text field.
  * `field.options | length` returns `true` if a Dropdown field.
  
  For some others, like the Lightswitch field, I don't know exactly how to guess the type properly.
  
  **Tags:** templating, field, fieldtypes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-15. Score: 5 | Accepted answer
  
  You can use the [className](https://docs.craftcms.com/v3/dev/functions.html#classname-object) function
  
  ```twig
  {% switch className(field) %}
      {% case 'craft\\fields\\PlainText' %}
      {% case 'craft\\fields\\Dropdown' %}
  
  ```
  
  
  ---
  
  