twig Basic template syntax Accessing variables


Example

In Twig templates variables can be accessed using double curly braces notation {{ variableName }}.

Basic example of greeting user

<!DOCTYPE html>
<html>
  <body>
    <span>Hello {{ name }}</span>
  </body>
</html>

Accessing array elements

Twig as a parameter can receive array. To access a specific element of array you can use regular php array access bracket notation {{ array[key] }}.

Previous example modified to use array as a parameter

<!DOCTYPE html>
<html>
  <body>
    <span>Hello {{ user['name'] }}</span>
  </body>
</html>

Accessing object properties

Objects also can be passed as a parameter to template. 'Dot' (.) notation is used to access specific object properties {{ object.propertyName }}.

Same example with object as a parameter

<!DOCTYPE html>
<html>
  <body>
    <span>Hello {{ user.name }}</span>
  </body>
</html>