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 6d 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 32% 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

4614d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/05d4ef6996da40b26351abbf683aa409f45b495217651f5d3eda3f5ea45de7e3?d=identicon)[eexit](/maintainers/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

[wyrihaximus/twig-view

Twig powered View for CakePHP

804.7M1](/packages/wyrihaximus-twig-view)[oro/twig-inspector

Oro Twig Inspector adds the possibility to find twig templates and blocks used for rendering HTML pages faster during development

47532.6k14](/packages/oro-twig-inspector)[shapecode/twig-template-event-bundle

Possibility to add code in a twig template dynamically

1010.7k](/packages/shapecode-twig-template-event-bundle)

PHPackages © 2026

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