You are here

protected function Twig_Node_Module::compileIsTraitable in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Node/Module.php \Twig_Node_Module::compileIsTraitable()
1 call to Twig_Node_Module::compileIsTraitable()
Twig_Node_Module::compileTemplate in vendor/Twig/Node/Module.php

File

vendor/Twig/Node/Module.php, line 298

Class

Twig_Node_Module
Represents a module node.

Code

protected function compileIsTraitable(Twig_Compiler $compiler) {

  // A template can be used as a trait if:
  //   * it has no parent
  //   * it has no macros
  //   * it has no body
  //
  // Put another way, a template can be used as a trait if it
  // only contains blocks and use statements.
  $traitable = null === $this
    ->getNode('parent') && 0 === count($this
    ->getNode('macros'));
  if ($traitable) {
    if ($this
      ->getNode('body') instanceof Twig_Node_Body) {
      $nodes = $this
        ->getNode('body')
        ->getNode(0);
    }
    else {
      $nodes = $this
        ->getNode('body');
    }
    if (!count($nodes)) {
      $nodes = new Twig_Node(array(
        $nodes,
      ));
    }
    foreach ($nodes as $node) {
      if (!count($node)) {
        continue;
      }
      if ($node instanceof Twig_Node_Text && ctype_space($node
        ->getAttribute('data'))) {
        continue;
      }
      if ($node instanceof Twig_Node_BlockReference) {
        continue;
      }
      $traitable = false;
      break;
    }
  }
  if ($traitable) {
    return;
  }
  $compiler
    ->write("public function isTraitable()\n", "{\n")
    ->indent()
    ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
    ->outdent()
    ->write("}\n\n");
}