PHP code example of eexit / twig-context-parser

1. Go to this page and download the library: Download eexit/twig-context-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eexit / twig-context-parser example snippets


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"
}
*/

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)
        }
      }
    }
  }
}