hasBlock example

public function hasBlock($name, array $context, array $blocks = [])
    {
        if (isset($blocks[$name])) {
            return $blocks[$name][0] instanceof self;
        }

        if (isset($this->blocks[$name])) {
            return true;
        }

        if (false !== $parent = $this->getParent($context)) {
            return $parent->hasBlock($name$context);
        }

        return false;
    }

    /** * Returns all block names in the current context of the template. * * This method checks blocks defined in the current template * or defined in "used" traits or defined in parent templates. * * @param array $context The context * @param array $blocks The current set of blocks * * @return array An array of block names */


    public function display(array $context = [])
    {
        // using func_get_args() allows to not expose the blocks argument         // as it should only be used by internal code         $this->template->display($context, \func_get_args()[1] ?? []);
    }

    public function hasBlock(string $name, array $context = []): bool
    {
        return $this->template->hasBlock($name$context);
    }

    /** * @return string[] An array of defined template block names */
    public function getBlockNames(array $context = []): array
    {
        return $this->template->getBlockNames($context);
    }

    public function renderBlock(string $name, array $context = []): string
    {

final class BlockTokenParser extends AbstractTokenParser
{
    public function parse(Token $token): Node
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();
        $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue();
        if ($this->parser->hasBlock($name)) {
            throw new SyntaxError(sprintf("The block '%s' has already been defined line %d.", $name$this->parser->getBlock($name)->getTemplateLine())$stream->getCurrent()->getLine()$stream->getSourceContext());
        }
        $this->parser->setBlock($name$block = new BlockNode($namenew Node([])$lineno));
        $this->parser->pushLocalScope();
        $this->parser->pushBlockStack($name);

        if ($stream->nextIf(/* Token::BLOCK_END_TYPE */ 3)) {
            $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
            if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) {
                $value = $token->getValue();

                


            $template = $twig->load($script->getName());

            if (!$hook instanceof FunctionHook) {
                $template->render(['hook' => $hook]);

                return;
            }

            $blockName = $hook->getFunctionName();
            if ($template->hasBlock($blockName)) {
                $template->renderBlock($blockName['hook' => $hook]);

                return;
            }

            if (!$hook instanceof OptionalFunctionHook) {
                throw new \RuntimeException(sprintf(
                    'Required function "%s" missing in script "%s", please make sure you add the required block in your script.',
                    $hook->getFunctionName(),
                    $script->getName()
                ));
            }
Home | Imprint | This part of the site doesn't use cookies.