2

I am attempting to add a slide-out menu to my Magento site, and I would like to inject the HTML by using the <?php echo $this->getChildHtml() ?> method. This menu would be universal to the entire desktop version of my website, so I want it to appear on all of the page templates (1column, 2column-right, 2-column-left, etc). I got the menu working perfectly on all templates by copy/pasting it into all of the aforementioned page templates, but this strikes me as bad coding practice—every time I edit the menu, I don't want to have to make the same change in 4 other files. That's why I want to use the getChildHtml() method; I would edit my menu.phtml and all layouts would be affected uniformly.

I followed the simple tutorial here but my getChildHtml('menu') is returning empty. I'll post my relevant code below. If someone could spot where I messed up—or failing that, suggest a different way to inject HTML dynamically (similar to JSP's <jsp:include page="..." /> method, maybe?)—I would really appreciate it. FYI I am using Magento CE 1.9.0.1.

Code:

template/theme = warrior/default

in app\design\frontend\warrior\default\layout\local.xml, I have:

<layout>
    <default>
        <reference name="root">
            <block type="core/text_list" name="menu" as="menu" translate="label">
                <label>Slide Menu</label>
            </block>
        </reference>

        <reference name="menu">
            <block type="core/template" name="menu" template="menu.phtml" />
        </reference>
    </default>
</layout>

in app\design\frontend\warrior\default\template\page\1column.phtml, I have (excerpt):

    <div class="main-container col1-layout">
        <div class="main">
            <?php echo $this->getChildHtml('breadcrumbs') ?>
            <div class="col-main">

                <?php echo $this->getChildHtml('global_messages') ?>
                <?php echo $this->getChildHtml('content') ?>

                <!-- My code -->
                <div> <?php echo $this->getChildHtml('menu') ?> </div> 

            </div>
        </div>
    </div>

in app\design\frontend\warrior\default\template\menu.phtml, I have:
<h1 style="background-color:yellow;"> sup dawg </h1>

And the resultant HTML in my browser is this: <div> </div> in the correct place, after the content block (I only have it in that specific spot to test this method. The actual menu code will go before the header).

Thanks in advance,
Ezra ♡

2

use the code as:

<layout version="0.1.0">
    <default>
        <reference name="root">
            <block type="core/template" name="menu" as="menu" template="menu.phtml" />
        </reference>
    </default>
</layout>

You don't need the second reference.

4
  • I think it's type="core/template", and a label isn't needed too ? Also an explanation of why the nesting is needed would be great
    – Niloct
    Jul 21 '14 at 17:32
  • MTM, do you mean that I should use this code in my local.xml instead of what I have? Or in addition to? Or inside the <layout> <default> ... </default> </layout> block of code? I am new to Magento and I would really appreciate a little more specificity :)
    – Ezratic
    Jul 21 '14 at 17:37
  • Okay, after trying it a few different ways, I got it to work with your code, wrapped by <layout> <default> ... </default> </layout>. Thanks! EDIT: only works using the type @Niloct provided.
    – Ezratic
    Jul 21 '14 at 17:40
  • @Niloct, i didn't tested the code, just wanted to tell to use template in root, don't need the second one. and yes the block type is to be core/template. will update the post
    – MTM
    Jul 21 '14 at 17:49

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.