Post

Working with Twig Variables Arrays and Objects

How does Twig work?

Consider the following code

1
{{ foo.bar }}
  • First check that foo is an array and bar is a valid element
  • If not, and foo is an object check that bar is a valid property
  • If not and foo is an object check that bar is a valid method
  • If not and foo is an object check that getBar is a valid method
  • If not and foo is an object check that isBar is a valid method
  • If the variable passed is a render array, if it was pre- rendered, output its #markup value, otherwise, call render() on it
  • If all checks fail, return a null value.

Say Something

print text or a region to the page

1
2
{{say_something}}
{{page.highlighted}}

Do Something - control structures

1
2
3
{% if page.footer_fifth%}
{{page.footer_fifth}}
{%endif%}

Comment Something

1
{# comment something out#}

Twig Filters

1
{{ name_of_variable|name_of_filter }}
check the page array object method for footer.fifth

Reference array

1
{{variable.1}} // this is the preferred "twig" way to get first element in array

however, if the array contains a pound sign or a dash ‘-‘ sign you need to use square bracket notation

1
{{variable[1]}}

to access an array multiple levels deep, keep using the dot syntax. use square brackets if the key contains a special character. (notice the dot is used after rather then before)

Twig arrays and objects

1
2
{{variable.key.another}}
{{variable.0['#hash'].key}}

{{ variable[‘#key’]}}

1
{{page.footer_fifth}}

Get value from paragraph Boolean Field

Execute from paragraph template

1
{% set stack = paragraph.field_boolean.value  %}

Loops

Iterate over key value pairs

1
2
3
4
5
 <ul class='blog-post__tags field__items'>
    {% for delta, value in items %}
      <li>{{delta}}: {{ value.content }}</li>
    {% endfor %}
  </ul>

Template inheritance

  • The extends keyword lets you “dress” a template in another template’s markup.
  • The block keyword defines the customizable area where another template’s code will be dropped in.
  • All the block tag does is to tell the template engine that a child template may override those portions of the template.
  • When a template uses extends, all markup is surrounded by block tags to define the custom markup.

Add to initial template file one that you want to use as the ‘master’.

1
{% block tabcontent %} {% endblock %}

Now you can use the code from your master template in another template file

1
{% extends 'template-name.twig' %}

Inspect variables in a theme

Best way to see what variables are available os to just use dump() with no params

1
2
3
4
{{ dump() }}

{{ dump(_context|keys) }}
dump(variable_key_name)

Missing desired Keys

If you are not getting all the expected keys and you are missing some vital keys for what you need to do. Then there’s probably some configuration issue with your display mode in your manage display settings. Ensure your display mode is configured correctly and all the desired fields are set to enabled in the content region.

More info

https://drupalize.me/tutorial/arrays-and-objects-twig?p=2512 see twig include_embed_extends see twig debugging blog

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.