/** * Compile the node. * * @param \Twig_Compiler $compiler A Twig compiler instance. */ public function compile(\Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $shortcode = $compiler->getEnvironment()->getShortcode($name); $filter = $compiler->getEnvironment()->getShortcodeFilter($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'shortcode'); $this->setAttribute('thing', $shortcode); $this->setAttribute('needs_environment', $shortcode->needsEnvironment()); $this->setAttribute('needs_context', $shortcode->needsContext()); $this->setAttribute('arguments', $shortcode->getArguments()); if ($shortcode instanceof \Twig_FunctionCallableInterface || $shortcode instanceof GenericShortcode) { $instance = ShortcodesTrait::getShortcodesClass(); $this->setAttribute('callable', $shortcode->getCallable()); } if ($this->hasNode('node')) { $body = $this->getNode('node'); if (!is_array($body)) { $body = [$body]; } $compiler->addDebugInfo($this)->write("ob_start();\n"); foreach ($body as $key => $node) { $compiler->subcompile($node); } $compiler->write("\$body = ob_get_clean();\n"); } if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) { $compiler->write("\$arguments = array("); foreach ($this->getNode('arguments') as $key => $node) { $compiler->string($key)->raw(" => ")->subcompile($node)->raw(", "); } $compiler->raw(");\n"); } $compiler->write('$compiled = $context["__shortcodes"](')->string($this->tag)->raw(", \$body, \$arguments);\n")->write($filter ? '$compiled = ' : 'echo '); $this->compileCallable($compiler); $compiler->raw(";\n"); // Filter shortcode, if registered filters are present if ($filter) { $compiler->write('echo $context["__shortcodes_filter"](')->string($name)->raw(', $compiled, $context, $this->env)'); } $compiler->raw(";\n")->write('unset($body, $arguments, $compiled);')->raw("\n"); }