0

Hi I try to parse a twig file template in PHP. I loop on twig node object excpecting find the raw filter of my template but does'nt appear inside the object. I can see other filter like escape or lower if I try on the same line. My code to parse twig :

<?php
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
 $source = new  \Twig\Source(file_get_contents($val->getPath() . '/' . $val->getFilename()), $val->getFilename(), $val->getPath());
$tree = $twig->parse($twig->tokenize($source));
$vv = $tree->getNodeTag();
$functions = [];
listFunctionCalls($tree, $functions);
function listFunctionCalls($node, array &$list) {
  if ($node instanceof \Twig\Node\Node) {

  }
  if ($node instanceof \Twig\Node\Expression\FilterExpression) {
    $filter = $node->getNode('filter');
    $value = $filter->getAttribute('value');
    if ($value == 'raw') {
      $a = 2;

    }
  }
  if ($node) {
    foreach ($node as $child) {
      listFunctionCalls($child, $list);
    }
  }
}

Here th twige template

{#
/**
 * @file
 * Theme override to display a two-column layout.
 *
 * Available variables:
 * - content: The content for this layout.
 * - attributes: HTML attributes for the layout <div>.
 */
#}
{% if content %}
 <div{{ attributes|raw|lower }}>

    {% if content.first %}
      <div {{ region_attributes.first.addClass('layout__region', 'layout__region-- first') }}>
        {{ content.first }}
      </div>
    {% endif %}

    {% if content.second %}
      <div {{ region_attributes.second.addClass('layout__region', 'layout__region--second') }}>
        {{ content.second }}
      </div>
    {% endif %}

  </div>
{% endif %}

Here the debug [1]: https://i.stack.imgur.com/CHfIQ.jpg Debug [1]][1]

Thomas Musa
  • 181
  • 1
  • 1
  • 8

1 Answers1

0

I found the problem i need to set the options optimizer to not skip the raw filter:

    $options = [
  'cache' => sys_get_temp_dir(),
  'optimizations' => \Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_VAR_ACCESS,
];

$env = new Environment($loader, $options);
Thomas Musa
  • 181
  • 1
  • 1
  • 8