Skip to main content

Magento 2 - Theme Layout and Templates


In magento 2, the theme folder is inside the module. We don't have to write the code in separate theme folder location outside the core module files folder.

Create a module as per the blog post. Create a view folder inside the module. The structure of the folder, is as given below.
VendorName
    ModuleName
        view
            adminhtml
                layout
                    sales_order_view.xml
                templates
                    sales
                        order
                            view
                                custom.phtml
            frontend
                layout
                    sales_order_view.xml
                templates
                    sales
                        order
                            view
                                custom.phtml

The theme for the adminhtml and frontend are now, inside the view folder. Magento will automatically process the layout and get the template files and add to the theme. The folder contains layout and templates same as the old magento theme folder. But, now the tags are replaced by individual xml files. Here, have used the sales_order_view.xml file to add custom block to the sales_order/view page.

Sample code for the sales_order_view.xml file.
<?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="order_totals">
            <block class="VendorName\ModuleName\Block\Adminhtml\Sales\Order\Custom" name="custom_order_totals" template="VendorName_ModuleName::sales/order/view/custom.phtml">
                <action method="setBeforeCondition">
                    <argument name="condition" xsi:type="string">tax</argument>
                </action>
            </block>
        </referenceBlock>
    </body>
</page>


The code for the custom.phtml will be similar to the magento 1 code.

Comments

Popular posts from this blog

Magento 2 - Create Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .