You are here

protected function Twig_TokenParser_For::checkLoopUsageBody in Translation template extractor 7.3

Same name and namespace in other branches
  1. 6.3 vendor/Twig/TokenParser/For.php \Twig_TokenParser_For::checkLoopUsageBody()
1 call to Twig_TokenParser_For::checkLoopUsageBody()
Twig_TokenParser_For::parse in vendor/Twig/TokenParser/For.php
Parses a token and returns a node.

File

vendor/Twig/TokenParser/For.php, line 96

Class

Twig_TokenParser_For
Loops over each item of a sequence.

Code

protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node) {
  if ($node instanceof Twig_Node_Expression_GetAttr && $node
    ->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node
    ->getNode('node')
    ->getAttribute('name')) {
    $attribute = $node
      ->getNode('attribute');
    if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute
      ->getAttribute('value'), array(
      'length',
      'revindex0',
      'revindex',
      'last',
    ))) {
      throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute
        ->getAttribute('value')), $node
        ->getLine(), $stream
        ->getFilename());
    }
  }

  // should check for parent.loop.XXX usage
  if ($node instanceof Twig_Node_For) {
    return;
  }
  foreach ($node as $n) {
    if (!$n) {
      continue;
    }
    $this
      ->checkLoopUsageBody($stream, $n);
  }
}