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