How can I get the type of a field using Twig? 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' %}
    &lt;select&gt;
        {% for option in field.options %}
            &lt;option value="{{ option.value }}"&gt;
                {{ option.label }}
            &lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
{% 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.
You can use the className function

{% switch className(field) %}
    {% case 'craft\\fields\\PlainText' %}
    {% case 'craft\\fields\\Dropdown' %}