2

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?

-1

There are few methods for achieving this. Simple one is as below.

Create file with name

> app/design/frontend/[VendorName]/[theme]/Magento_<module youwantto
> override>/templates/<your phtml file path>

Do your customization there

Same way Create xml file with same name as base module file in

app/design/frontend/[VendorName]/[theme]/Magento_<module youwantto override>/
layout/<your modulefilexml eg. catalog_product_view.xml>

and add your changes there.

let me know if you need further help.

  • Further help please! What layout code do I use in the XML file? In the phtml file do I still use $this->getChildHtml() - I understand the file structure in Magento 2, what I need information on is the code (mostly the XML) that I need to put into those files – Jimmery Apr 13 '16 at 15:10
  • You can check core phtml files if this is supported or not For eg. to display filter state, it is done like this " echo $block->getChildHtml('state')" Layout is bit different but mostly its same. Check dev doc for more information on theme part. mark my answer accepted if it helped. – Pallavi Apr 14 '16 at 7:47
  • sorry Pallavi, I was asking how to insert a block into a custom place, and all your answer contains is where to create some files and a comment saying "check dev doc". Until you can answer my original question with something less vague I cannot mark you as correct. – Jimmery Apr 21 '16 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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