insertusernamehere
insertusernamehere

Reputation: 23610

Is there any difference using quotes on defining a variable or not?

The Twig manual ("include") says this:

{% include 'foo' with {'foo': 'bar'} %}

But this works fine too:

{% include 'foo' with { foo: 'bar'} %}

So is there any difference or are the quotes arbitrary?

Upvotes: 1

Views: 113

Answers (1)

lifo
lifo

Reputation: 2893

The previous answer by @Maerlyn is not completely accurate.

As of Twig 1.5 you can use unquoted strings as the key name in hashes. For example {foo: 'bar'} is the same as {'foo': 'bar'} even if you had a variable named foo in your template it would not clash with the hash key name of foo. It's a convenience thing, that's all.

For example:

{% set foo = 'bar' %}
{% set bar = {foo: foo} %} {# note: no quotes around foo #}
{% debug bar %}

Expected output:

array
  'foo' => string 'bar' (length=3)

Upvotes: 1

Related Questions