2

I want to use the features of default_head_blocks.xml in a custom block that will not be rendered for output, but saved in a file.

As far as I understand it, the HTML head is now rendered completely based on layout XML, it's not a block class anymore. But is there a sensible way to reuse <head> rendering without rendering a complete page?

2

What I found out:

  • the head is rendered in the root template vendor/magento/module-theme/view/base/templates/root.phtml

    <head <?php /* @escapeNotVerified */ echo $headAttributes ?>>
        <?php /* @escapeNotVerified */ echo $requireJs ?>
        <?php /* @escapeNotVerified */ echo $headContent ?>
        <?php /* @escapeNotVerified */ echo $headAdditional ?>
    </head>
    

    To use it separated from <body> we need to create our own root template with only this part.

  • The variables are assigned in \Magento\Framework\View\Result\Page::render() which also renders the root template into a given Response object and inserts the rendered layout into the <body> element.

    $this->assign([
        'requireJs' => $requireJs ? $requireJs->toHtml() : null,
        'headContent' => $this->pageConfigRenderer->renderHeadContent(),
        'headAdditional' => $addBlock ? $addBlock->toHtml() : null,
        'htmlAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_HTML),
        'headAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_HEAD),
        'bodyAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_BODY),
        'loaderIcon' => $this->getViewFileUrl('images/loader-2.gif'),
    ]);
    

    If we don't want the complete layout and a response, we have to do the same on our own. Using PageConfigRenderer should not be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.