PHPackages                             eexit/twig-context-parser - 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. eexit/twig-context-parser

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

eexit/twig-context-parser
=========================

Get a Twig template context variables pragmatically.

0.1.0(12y ago)240MITPHPPHP &gt;=5.3.3

Since Sep 28Pushed 12y ago1 watchersCompare

[ Source](https://github.com/eexit/twig-context-parser)[ Packagist](https://packagist.org/packages/eexit/twig-context-parser)[ Docs](http://www.eexit.net)[ RSS](/packages/eexit-twig-context-parser/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Twig Context Parser [![Build Status](https://camo.githubusercontent.com/0f7dadf6e8999bd079315289994b0fc63c72b73bee0bd6aef81ba44a0dcb633a/68747470733a2f2f7472617669732d63692e6f72672f65657869742f747769672d636f6e746578742d7061727365722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/eexit/twig-context-parser)
==============================================================================================================================================================================================================================================================================================================================

[](#twig-context-parser-)

This small script uses the internal Twig tokenize/parser/compiler to obtain a template variable out of its rendering context.

If you are building a flat file Twig CMS based, it might be very useful to work using parsed flat file variable. There are many uses.

This parser basically return all your variables set in Twig using the block `set` as complex as it might be:

```
{% set foo = [
    {"bar":"baz"},
    "bar",
    range(0, 12, 2),
    ["yux", {
        "baz":"yea",
        "bar":"foo",
        "range":range(0, 100)
    }]
] %}

```

Will output:

```
array(1) {
  'foo' =>
  array(4) {
    [0] =>
    array(1) {
      'bar' =>
      string(3) "baz"
    }
    [1] =>
    string(3) "bar"
    [2] =>
    array(7) {
      [0] =>
      int(0)
      [1] =>
      int(2)
      ...
      [6] =>
      int(12)
    }
    [3] =>
    array(2) {
      [0] =>
      string(3) "yux"
      [1] =>
      array(3) {
        'baz' =>
        string(3) "yea"
        'bar' =>
        string(3) "foo"
        'range' =>
        array(101) {
          [0] =>
          int(0)
          [1] =>
          int(1)
          ...
          [99] =>
          int(99)
          [100] =>
          int(100)
        }
      }
    }
  }
}

```

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

[](#installation)

Supposing you are using [composer](http://getcomposer.org), add to your `composer.json`:

```
{
    "require": {
        "eexit/twig-context-parser": "0.1.*"
    }
}
```

Usage
-----

[](#usage)

```
use Eexit\Twig\ContextParser\ContextParser;

$loader = new \Twig_Loader_String();
$twig = new \Twig_Environment($loader);

$template = $twig->parse($twig->tokenize('{% set foo = "bar" %}{% set baz = "yux" %}'));

$context = new ContextParser($twig);

var_dump($context->parse($template)->getContext());

/*
array(2) {
    'foo' =>
    string(3) "bar"
    'baz' =>
    string(3) "yux"
}
*/
```

The `ContextParser::getParser()` will return the node context once. Once you called the method, calling it again will return nothing. This allows to use the same `ContextParser` instance to parse several template in a row.

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

4659d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/57098?v=4)[Joris Berthelot](/maintainers/eexit)[@eexit](https://github.com/eexit)

---

Top Contributors

[![eexit](https://avatars.githubusercontent.com/u/57098?v=4)](https://github.com/eexit "eexit (2 commits)")

---

Tags

phptwigtwig-context-parsertwig-extensiontwigparserContexttemplatevariable

### Embed Badge

![Health badge](/badges/eexit-twig-context-parser/health.svg)

```
[![Health](https://phpackages.com/badges/eexit-twig-context-parser/health.svg)](https://phpackages.com/packages/eexit-twig-context-parser)
```

###  Alternatives

[symfony/ux-twig-component

Twig components for Symfony

22018.6M342](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1647.0M126](/packages/symfony-ux-live-component)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

16126.1k1](/packages/symfony-ux-toolkit)[mati365/ckeditor5-symfony

CKEditor 5 integration for Symfony

262.6k](/packages/mati365-ckeditor5-symfony)

PHPackages © 2026

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