Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-11T10:34:05.933Z
Answers.LastActivityDate: 2014-07-11T10:34:05.933Z
Answers.UserDisplayName: Jérôme Coupé
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-25T14:07:43.037Z
Answers.Text: very nice , i didn t know u can regex in twig
Answers.UserDisplayName: Charles-Antoine Fournel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T12:49:45.090Z
Answers.LastActivityDate: 2014-07-29T13:06:44.440Z
Answers.LastEditDate: 2014-07-29T13:06:44.440Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-06-14T16:14:51.793Z
Answers.LastActivityDate: 2019-11-11T11:46:16.330Z
Answers.LastEditDate: 2019-11-11T11:46:16.330Z
Answers.UserDisplayName: Kevinleary.net
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-25T19:26:27.650Z
Answers.LastActivityDate: 2024-07-25T19:26:27.650Z
Answers.UserDisplayName: Jason
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T16:37:19.403Z
Answers.Text: Very nice, Victor!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T16:28:15.150Z
Answers.LastActivityDate: 2014-07-29T16:28:15.150Z
Answers.UserDisplayName: Victor
Body: <p>How do I check what type my variable of (string, integer, boolean, array, dictionary,  etc)?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-11T17:36:39.640Z
Comments.Text: 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
Comments.UserDisplayName: Ian
Tags: <templating><variable><variable-type>
Title: How to check variable type?
ThreadText: # 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](https://github.com/username_7-in/Craft-TwigTypeTest)
  
  **Example:**
  
  ```twig
  {% set test_var = craft.entries %}
  
  {% if test_var is of_type('object') %}
    true
  {% endif %}
  
  ```
  
  or
  
  ```twig
  {% set test_var = craft.entries %}
  
  {{ test_var|get_type }}
  {# Outputs: object #}
  
  ```
  
  > **Comments:**
  > **username_3**: Very nice, username_7!  
  
  ---
  
  ### 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:
  
  ```twig
  {# 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`](http://twig.sensiolabs.org/doc/templates.html#comparisons) 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](https://github.com/fabpot/Twig/pull/1457) (or put otherwise: [wrong documentation](http://twig.sensiolabs.org/doc/templates.html#comparisons) of using regex here).
  
  ```twig
  {# 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 %}
  
  ```
  
  
  ---
  
  