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

CC BY-SA 4.0
2018-08-15T14:19:53.377Z
2018-08-15T14:19:53.377Z
Robin Schambach
<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>

<templating><field><fieldtypes>
How can I get the type of a field using Twig?
# 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' %}
    
        {% for option in field.options %}
            
                {{ option.label }}
            
        {% endfor %}
    
{% 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 function
{% switch className(field) %}
    {% case 'craft\\fields\\PlainText' %}
    {% case 'craft\\fields\\Dropdown' %}


---

