Drupal 8/9: List available variables on a Twig file
03/02/2021 by admin

Front.id

Here is a quick alternative to see which variables are available in a twig template when you don't have a debugger like xdebug.

It is especially useful when you want to verify that certain values are available to be printed in a particular template.

Simply put this snippet in the twig file you want to scan and it will print a list of the available variables (they will be printed in the position where you put the snippet):

<ol>
  {% for key, value in _context  %}
    <li>{{ key }}</li>
  {% endfor %}
</ol>

 

Result: 

Sample snippet printed variables twig file

Bye!

Tags

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.