Reputation: 10139
In Magento 1
I can edit local.xml
like so:
<default>
<reference name="root">
<block type="core/template" name="above_main" template="page/html/banner.phtml" />
</reference>
</default>
I can edit a template file like so:
<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<?php echo $this->getChildHtml('after_body_start') ?>
<div class="wrapper">
<?php echo $this->getChildHtml('global_notices') ?>
<div class="page">
<?php echo $this->getChildHtml('header') ?>
<?php // MY EDIT: ?>
<?php echo $this->getChildHtml('above_main');
<div class="main-container col2-left-layout">
<div class="main">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<!-- rest of page... -->
This will end up with the file at page/html/banner.phtml
being inserted into the template at my own custom position, above_main
.
OK, so my question is:
How do I do this in Magento 2?
Upvotes: 1
Views: 16641
Reputation: 2298
There is no more local.xml in Magento 2. Magento has instead chosen to have all of the XML within the module. If you want to modify XML for a module, you will have to make a new XML file with the same name in your equivalent theme directory and make the modifications there.
There are many possible ways to insert your template into a particular page.
Alternative 1
Let's assume you want to add a template on product details page. You need to create new xml file catalog_product_view.xml
in your module. You xml code might look like this:
<?xml version="1.0"?>
<body>
<referenceContainer name="product.info.main">
<block class="Lapisbard\Catalog\Block\Product\View\Extra"
name="product.view.extra"
template="Lapisbard_Catalog::product/view/extra.phtml"
after="product.info.overview">
</block>
</referenceContainer>
</body>
Here I'm inserting a custom template on product page. You can use before/after
tag to arrange your container according to your need of where you want to place your template. Similar way you can create layout files in your module account to handle of the page. Ex: you can use cms_index_index.xml
for home page.
Alternative 2
You can call your custom template file in any other template file using following code:
echo $this->getLayout()
->createBlock('Lapisbard\Catalog\Block\Product\View\Extra')
->setTemplate('Lapisbard_Catalog::product/view/extra.phtml')
->toHtml();
Alternative 3
Have a look at this tutorial which highlights how you can utilize default.xml to make structural changes https://gielberkers.com/creating-blocks-in-magento-2/
Upvotes: 9