PHPackages                             qeep-pro/twig-tree-tag - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. qeep-pro/twig-tree-tag

ActiveLibrary[Templating &amp; Views](/categories/templating)

qeep-pro/twig-tree-tag
======================

A Twig extension for succinctly traversing nested lists (e.g. navigation menus).

v3.0.0(5mo ago)34.1k↑33.3%MITPHP

Since Feb 15Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/QEEP-Pro/twig-tree-tag)[ Packagist](https://packagist.org/packages/qeep-pro/twig-tree-tag)[ Docs](https://github.com/QEEP-Pro/twig-tree-tag)[ RSS](/packages/qeep-pro-twig-tree-tag/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (8)Used By (0)

twig-tree-tag
=============

[](#twig-tree-tag)

A Twig extension for succinctly traversing nested lists (e.g. navigation menus).

Requirements
------------

[](#requirements)

Requires PHP 5.4 or higher (due to usage of `$this` in anonymous function of compiled templates -- unfortunately this is the only way to achieve the desired recursion of a block calling itself).

Idea
----

[](#idea)

The `{% tree %}` tag works almost like `{% for %}`, but inside a `{% tree %}` you can call `{% subtree var %}` to recursively run your `{% tree %}` block with the given `var`. The primary use-case for this tag is nested navigation menus.

This extension was written by [Alain Tiemblo](https://github.com/ninsuo), (with a few very minor changes by [Jordan Lev](https://github.com/jordanlev)). Adapted for Twig 2 by [Igor Kamyshev](https://github.com/TheNovel).

Usage Example
-------------

[](#usage-example)

In this example, `menu` is an array of objects, each containing `name`, `url`, and `children` properties (`children` is itself an array of objects with the same properties, etc).

```
{% tree item in menu %}
  {% if treeloop.first %}{% endif %}

        {{ item.name }}
        {% subtree item.children %}

  {% if treeloop.last %}{% endif %}
{% endtree %}
```

Just like a `{% for %}` loop, you can access the key of each list item:

```
{% tree key, item in menu %}

    Item {{ key }}: {{ item.name }}
    {% subtree item.children %}

{% endtree %}
```

What is the `treeloop` var?
---------------------------

[](#what-is-the-treeloop-var)

The `treeloop` var serves the same purpose inside a `{% tree %}` tag as the `loop` var does inside a `{% for %}` tag. It is named differently so that you can still use `loop` when you have a `{% for %}` tag inside your `{% tree %}` tag (otherwise they would conflict).

`treeloop` contains all the same [special variables as `loop`](http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable):

- `treeloop.index`: The current iteration of the loop *within the current nesting level*. (1 indexed)
- `treeloop.index0`: The current iteration of the loop *within the current nesting level*. (0 indexed)
- `treeloop.revindex`: The number of iterations from the end of the loop *within the current nesting level* (1 indexed)
- `treeloop.revindex0`: The number of iterations from the end of the loop *within the current nesting level* (0 indexed)
- `treeloop.first`: True if first iteration *of the current nesting level*
- `treeloop.last`: True if last iteration *of the current nesting level*
- `treeloop.length`: The number of items in the sequence *of the current nesting level*
- `treeloop.parent`: The context of the parent nesting level (or the parent context of the `tree` tag itself if currently at the root level of the tree).

Additionally, `treeloop` also contains 2 extra variables that tell you about the current nesting level:

- `level`: The current nesting level (1 indexed -- so root level of the tree is 1, 2nd-level is 2, etc)
- `level0`: The current nesting level (0 indexed -- so root level of the tree is 0, 2nd level is 1, etc)

What if I want a tree tag inside another tree tag?
--------------------------------------------------

[](#what-if-i-want-a-tree-tag-inside-another-tree-tag)

To handle the edge case where you want to start a new tree inside another tree (that is, a new tree "root" with its own markup), use `as` in your `{% tree %}` tag to assign each tree to a var name, then pass it into `subtree` via `with`. This allows Twig to know which `{% tree %}` should be called when it comes across the `{% subtree %}` tag. For example...

```
{% tree item in menu as treeA %}
  {% if treeloop.first %}{% endif %}

        {{ item.name }}
        {% subtree item.children with treeA %}

        Some other tree (that has its own "root", not a sub-tree of treeA):
        {% tree otherthing in item.otherthings as treeB %}
          {{ otherthings.name }}
          {% subtree otherthings.subitems with treeB %}
          {# We use "with treeB" above so Twig knows which parent tree tag to call #}
        {% endtree %}

  {% if treeloop.last %}{% endif %}
{% endtree %}
```

Installation
------------

[](#installation)

```
composer require qeep-pro/twig-tree-tag
```

Usage
-----

[](#usage)

### Standalone

[](#standalone)

```
$loader = require __DIR__.'/vendor/autoload.php';

$twig = new \Twig_Environment(
    new \Twig_Loader_Filesystem(__DIR__.'/view/')
);

$twig->addExtension(new QEEP\TwigTreeTag\Twig\Extension\TreeExtension());

// (...)
```

### Symfony 3

[](#symfony-3)

Add following lines to `services.yml`:

```
app.twig.tree_extension:
  class: QEEP\TwigTreeTag\Twig\Extension\TreeExtension
  tags:
    - { name: twig.extension }
```

License
-------

[](#license)

The MIT License (MIT)

Please read the [LICENSE](LICENSE) file for more details.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance71

Regular maintenance activity

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 61.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~641 days

Recently: every ~716 days

Total

6

Last Release

165d ago

Major Versions

v1.0.3 → v2.0.02018-01-26

v2.0.0 → v3.0.02025-11-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/eb826bf7775f0574a417b011442f33dd0ec6c593ac2a10b7c7402520a5e4eaa4?d=identicon)[igor.kamyshev](/maintainers/igor.kamyshev)

---

Top Contributors

[![ninsuo](https://avatars.githubusercontent.com/u/4152132?v=4)](https://github.com/ninsuo "ninsuo (16 commits)")[![igorkamyshev](https://avatars.githubusercontent.com/u/26767722?v=4)](https://github.com/igorkamyshev "igorkamyshev (9 commits)")[![sivolobov](https://avatars.githubusercontent.com/u/1647174?v=4)](https://github.com/sivolobov "sivolobov (1 commits)")

### Embed Badge

![Health badge](/badges/qeep-pro-twig-tree-tag/health.svg)

```
[![Health](https://phpackages.com/badges/qeep-pro-twig-tree-tag/health.svg)](https://phpackages.com/packages/qeep-pro-twig-tree-tag)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
