Magento 2 – Add Custom Template File into Head

csslayoutmagento2

In magento 1.x, I can add the css files into the head using helper like below code.

<reference name="head">
    <action method="addCss"><stylesheet helper="module/helperclass/helperfunction"/></action>
</reference>

But cannot do this on Magento 2.

So now, I added this code <link rel="stylesheet" type="text/css" media="all" href="<?php echo $_helper->getCSSFile()?>"> into "after.body.start" container.

Anyone know how can I add custom template file into <head>?

Best Answer

If you want add css file into head you can use this code:

<head>
   <css src="path_to/file.css" />
</head>

But if you need to add custom block into head, you can use this code:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="block_name" template="path_to_file.phtml" />
        </referenceBlock>
    </body>
</page>

Hope this helps

Related Topic