<p>To my knowledge this is not possible natively in Twig. Depending on your situation / template needs here are some useful tests</p>

<p><code>{% if foo is iterable %}</code></p>

<p>Array or object</p>

<p><code>{% if foo is same as(true) %}</code></p>

<p>Basically identical to using === in php</p>

CC BY-SA 3.0
2014-07-11T10:34:05.933Z
2014-07-11T10:34:05.933Z
Jérôme Coupé
<p>I recently needed to check if a variable is a number. My first idea was to use the <code>divisibleby</code> test:</p>



<pre class="lang-twig prettyprint-override"><code>{# DOESN'T WORK AS EXPECTED! #}
{% if var is divisible by(1) %}
{% endif %}
</code></pre>

<p>Unfurtunatelly twig returns <code>true</code> for every string passed to it.</p>

<p>But what you can do is to use regular expressions using the <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons"><code>matches</code></a> operator in the comparison. You'll note that I double escaped the dot and the meta-character <code>\d</code>, this is because of a <a href="https://github.com/fabpot/Twig/pull/1457">twig bug</a> (or put otherwise: <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons">wrong documentation</a> of using regex here).</p>

<pre class="lang-twig prettyprint-override"><code>{# Match integer #}
{% if var matches '/^\\d+$/' %}
{% endif %}

{# Match floating point number #}
{% if var matches '/^[-+]?[0-9]*\\.?[0-9]+$/' %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-02-25T14:07:43.037Z
very nice , i didn t know u can regex in twig
Charles-Antoine Fournel
CC BY-SA 3.0
2014-07-29T12:49:45.090Z
2014-07-29T13:06:44.440Z
2014-07-29T13:06:44.440Z
carlcs
<p>I hit a snag for this where I needed to check if a value was a boolean type. This isn't the greatest approach, but it does work and is easy to interpret:</p>

<pre><code>{% if value is same as(false) or value is same as(true) %}
</code></pre>

<p>Documentation: <a href="https://twig.symfony.com/doc/2.x/tests/sameas.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/tests/sameas.html</a></p>

CC BY-SA 4.0
2016-06-14T16:14:51.793Z
2019-11-11T11:46:16.330Z
2019-11-11T11:46:16.330Z
Kevinleary.net
<p>In the current version of Craft/Twig (Craft 5) you can do the following:</p>
<pre><code>{% if value is integer or value is float %}
...
{% endif %}
</code></pre>

CC BY-SA 4.0
2024-07-25T19:26:27.650Z
2024-07-25T19:26:27.650Z
Jason
<p>I needed this feature today, so I've put together a simple plugin, which does the job: <a href="https://github.com/victor-in/Craft-TwigTypeTest">Craft-TwigTypeTest</a></p>

<p><strong>Example:</strong></p>

<pre class="lang-twig prettyprint-override"><code>{% set test_var = craft.entries %}

{% if test_var is of_type('object') %}
  true
{% endif %}
</code></pre>

<p>or</p>

<pre class="lang-twig prettyprint-override"><code>{% set test_var = craft.entries %}

{{ test_var|get_type }}
{# Outputs: object #}
</code></pre>

CC BY-SA 3.0
2014-07-29T16:37:19.403Z
Very nice, Victor!
carlcs
CC BY-SA 3.0
2014-07-29T16:28:15.150Z
2014-07-29T16:28:15.150Z
Victor
<p>How do I check what type my variable of (string, integer, boolean, array, dictionary,  etc)?</p>

CC BY-SA 3.0
2014-07-11T17:36:39.640Z
Your solution may be the best one so far. It looks as if Twig has rejected adding this feature as it doesn't fall inline with the goals of Twig. https://github.com/fabpot/Twig/issues/595
Ian
<templating><variable><variable-type>
How to check variable type?
# How to check variable type?

(Asked by: username_0 on 2014-07-11)

How do I check what type my variable of (string, integer, boolean, array, dictionary, etc)?

**Tags:** templating, variable, variable-type
> **Comments:**
> **username_1**: Your solution may be the best one so far. It looks as if Twig has rejected adding this feature as it doesn't fall inline with the goals of Twig. https://github.com/fabpot/Twig/issues/595  

---

## 5 answers

### Answer by username_7 on 2014-07-29. Score: 9 | Accepted answer

I needed this feature today, so I've put together a simple plugin, which does the job: Craft-TwigTypeTest
Example:
{% set test_var = craft.entries %}

{% if test_var is of_type('object') %}
  true
{% endif %}

or
{% set test_var = craft.entries %}

{{ test_var|get_type }}
{# Outputs: object #}

> **Comments:**
> **username_3**: Very nice, Victor!  

---

### Answer by username_2 on 2014-07-11. Score: 14

To my knowledge this is not possible natively in Twig. Depending on your situation / template needs here are some useful tests

`{% if foo is iterable %}`

Array or object

`{% if foo is same as(true) %}`

Basically identical to using === in php


---

### Answer by username_3 on 2014-07-29. Score: 7

I recently needed to check if a variable is a number. My first idea was to use the divisibleby test:
{# DOESN'T WORK AS EXPECTED! #}
{% if var is divisible by(1) %}
{% endif %}

Unfurtunatelly twig returns true for every string passed to it.
But what you can do is to use regular expressions using the matches operator in the comparison. You'll note that I double escaped the dot and the meta-character \d, this is because of a twig bug (or put otherwise: wrong documentation of using regex here).
{# Match integer #}
{% if var matches '/^\\d+$/' %}
{% endif %}

{# Match floating point number #}
{% if var matches '/^[-+]?[0-9]*\\.?[0-9]+$/' %}
{% endif %}

> **Comments:**
> **username_4**: very nice , i didn t know u can regex in twig  

---

### Answer by username_5 on 2016-06-14. Score: 4

I hit a snag for this where I needed to check if a value was a boolean type. This isn't the greatest approach, but it does work and is easy to interpret:
{% if value is same as(false) or value is same as(true) %}

Documentation: https://twig.symfony.com/doc/2.x/tests/sameas.html


---

### Answer by username_6 on 2024-07-25. Score: 1

In the current version of Craft/Twig (Craft 5) you can do the following:
{% if value is integer or value is float %}
...
{% endif %}


---

