Magento 2 Custom Module – Fixing Invalid Template File Error

magento2

Issue

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'TT::calculator.phtml' in module: 'Magento_Catalog' block's name: 'calculator.tab'

Layout Location:

app/code/TT/Calculator/view/frontend/layout/catalog_product_view.xml

<?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.details">
        <block class="Magento\Catalog\Block\Product\View" name="calculator.tab" template="TT::calculator.phtml" group="detailed_info">
            <arguments>
                <argument translate="true" name="title" xsi:type="string">Calculator</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>
</page>

Template Location:

app/code/TT/Calculator/view/frontend/templates/calculator.phtml

Questions

  1. I've seen some solutions where the *.phtml file is saved in app/design but I'm trying to get this to work in app/code. What have I missed?
  2. Also, does this "extend" catalog_product_view.xml or replace it?

Best Answer

1.You have to change the code to TT_Calculator::calculator.phtml:

<?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.details">
        <block class="Magento\Catalog\Block\Product\View" name="calculator.tab" template="TT_Calculator::calculator.phtml" group="detailed_info">
            <arguments>
                <argument translate="true" name="title" xsi:type="string">Calculator</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>
</page>

2.Magento will check all the layout (XML file) in all the modules and merge them, not extend.