1

I want to add my custom template on product page just like $this->getChildHtml('custom_product_info').

As in magento1.9 to use $this->getChildHtml('custom_product_info') we add layout xml like below :

<catalog_product_view>
    <reference name="product.info"> 
        <block type="core/template"  name="custom_product_info" template="custom.phtml"/>  
    </reference>
</catalog_product_view>

What is the xml and process to add custom template in Magento2.1?

5 Answers 5

5

Try this:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info">
            <block class="{Vendor}\{Module}\Block\Catalog\Product\View\{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
                <!-- If you have any arguments to pass -->
                <arguments>
                    <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>
4
  • For class="{Vendor}\{Module}\Block\Catalog\Product\View\{Class}" should I create a block class, please post a example for block class. Mar 26, 2018 at 6:26
  • You should be able to use this block: Magento\Framework\View\Element\Template
    – hungersoft
    Mar 26, 2018 at 6:27
  • How can I get $product in custom template file? Mar 26, 2018 at 9:28
  • Follow this tutorial: blog.chapagain.com.np/…
    – hungersoft
    Mar 26, 2018 at 9:30
2

If you want to add this custom block via Custom module, Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceBlock name="product.info">
            <block class="{Vendor}\{Module}\Block\{BLOCK_CLASSS}" name="{block.name.inlayout}"  as="{name_aliase_path}"
template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
            </block>
        </referenceBlock>
    </body>
</page>

Then you can call your block via at code at form.phtml at

<?= $block->getChildHtml({name_aliase_path}) ?>
1
  • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
    – inrsaurabh
    Mar 26, 2018 at 6:59
1

Use this

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceBlock name="product.info">
            <block class="Magento\Framework\View\Element\Template" name="block.name"  as="block.alias"
template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
            </block>
        </referenceBlock>
    </body>
</page>
0
 <referenceContainer name="product.info.main">
        <block class="Magento\Framework\View\Element\Template" name="{argument_name}" template="template_name.phtml"/>
    </referenceContainer>
0
0
<referenceContainer name="content">

            <block class="Xcommerce\Category\Block\Overview" name="overview" 
            template="X2commerce_Category::custom.phtml">
        </block>
</referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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