70

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 ?

hsz
  • 148,279
  • 62
  • 259
  • 315
  • 4
    You can maybe do something with the `iterable` test? http://twig.sensiolabs.org/doc/tests/iterable.html – Wouter J Dec 16 '12 at 14:48

5 Answers5

152

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

Mario
  • 8,253
  • 2
  • 14
  • 29
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 1
    Note: other objects can also be `iterable` even though they are not an `array`. Iterable is not a true test if something is an array. I have an answer how to make an array test like `{% if value is array %}` below. http://stackoverflow.com/a/29642662/417822 – Sir.Nathan Stassen Apr 30 '15 at 16:37
  • 1
    Strings are iterable. I'll be posting **a** solution shortly. **It** might not be enough. – Darren Embry Jul 28 '21 at 20:06
15

Ok, I did it with:

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

Ugly, but works.

hsz
  • 148,279
  • 62
  • 259
  • 315
15

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 % }
Sir.Nathan Stassen
  • 1,843
  • 4
  • 21
  • 25
  • 2
    A sidenote here : This test will return false for every custom object implementing ArrayAcces, Traverseable, ... – DarkBee May 01 '15 at 09:09
  • 1
    Right, but the core issue is that not all `iterable` items are equal. The structure of an `iterable` can look different depending on type. – Sir.Nathan Stassen Jun 10 '15 at 16:50
  • 1
    Where this class file should be put in Symfony2? – Aerendir Aug 14 '15 at 12:27
  • 1
    Not sure for Symfony2. You'll need to find a spot during your app boot or early on in a controller - but after twig has been loaded. – Sir.Nathan Stassen Aug 14 '15 at 13:43
  • 2
    You put your test into a TwigExtension subclass (in the `getTests` method), register it as a service, and add the tag `twig.extension` to the service definition. The tag is what makes the TwigBundle take care of the `addExtension` for you. More details here: http://symfony.com/doc/current/templating/twig_extension.html – Olivier 'Ölbaum' Scherler May 15 '17 at 06:44
4

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;
    }
}
wtorsi
  • 692
  • 2
  • 8
  • 27
-3

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.

Darren Embry
  • 155
  • 1
  • 2