62
votes

Is it possible to check if given variable is string in Twig ?

Expected solution:

messages.en.yml:

hello:
  stranger: Hello stranger !
  known: Hello %name% !

Twig template:

{% set title='hello.stranger' %}
{% set title=['hello.known',{'%name%' : 'hsz'}] %}

{% if title is string %}
  {{ title|trans }}
{% else %}
  {{ title[0]|trans(title[1]) }}
{% endif %}

Is it possible to do it this way ? Or maybe you have better solution ?

5
You can maybe do something with the iterable test? twig.sensiolabs.org/doc/tests/iterable.html - Wouter J

5 Answers

140
votes

Can be done with the test iterable, added in twig1.7, as Wouter J stated in the comment :

{# evaluates to true if the users variable is iterable #}
{% if users is iterable %}
    {% for user in users %}
        Hello {{ user }}!
    {% endfor %}
{% else %}
    {# users is probably a string #}
    Hello {{ users }}!
{% endif %}

Reference : iterable

12
votes

I found iterable to not be good enough since other objects can also be iterable, and are clearly different than an array.

Therefore adding a new Twig_SimpleTest to check if an item is_array is much more explicit. You can add this to your app configuration / after twig is bootstrapped.

$isArray= new Twig_SimpleTest('array', function ($value) {
    return is_array($value);
});
$twig->addTest($isArray);

Usage becomes very clean:

{% if value is array %}
    <!-- handle array -->
{% else %}
    <!-- handle non-array -->
{% endif % }
11
votes

Ok, I did it with:

{% if title[0] is not defined %}
    {{ title|trans }}
{% else %}
    {{ title[0]|trans(title[1]) }}
{% endif %}

Ugly, but works.

3
votes

There is no way to check it correctly using code from the box. It's better to create custom TwigExtension and add custom check (or use code from OptionResolver).

So, as the result, for Twig 3, it will be smth like this

class CoreExtension extends AbstractExtension
{
    public function getTests(): array
    {
        return [
            new TwigTest('instanceof', [$this, 'instanceof']),
        ];
    }

    public function instanceof($value, string $type): bool
    {
        return ('null' === $type && null === $value)
               || (\function_exists($func = 'is_'.$type) && $func($value))
               || $value instanceof $type;
    }
}
-1
votes

Assuming you know for a fact that a value is always either a string or an array:

{% if value is iterable and value is not string %}
    ...
{% else %}
    ...
{% endif %}

This worked good enough for me in a project I was working on. I realize you may need another solution.