2017 © Pedro Peláez
 

library twig-context-parser

Get a Twig template context variables pragmatically.

image

eexit/twig-context-parser

Get a Twig template context variables pragmatically.

  • Saturday, September 28, 2013
  • by eexit
  • Repository
  • 1 Watchers
  • 2 Stars
  • 27 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Twig Context Parser Build Status

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

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., (*2)

This parser basically return all your variables set in Twig using the block set as complex as it might be:, (*3)

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

Will output:, (*4)

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

Supposing you are using composer, add to your composer.json:, (*5)

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

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., (*6)

The Versions

28/09 2013

dev-master

9999999-dev http://www.eexit.net

Get a Twig template context variables pragmatically.

  Sources   Download

MIT

The Requires

 

by Joris Berthelot

twig parser template variable context

28/09 2013

0.1.0

0.1.0.0 http://www.eexit.net

Get a Twig template context variables pragmatically.

  Sources   Download

MIT

The Requires

 

by Joris Berthelot

twig parser template variable context