0

So there's one piece of the custom block puzzle I'm not really getting.

What I'm trying to do:

I want to use $this->getChildHtml('customBlock') inside my header.phtml file and have it just print 'Hello World'

What I have thusfar:

An xml file called app/etc/modules/ Jiri_CustomBlock.xml with the following contents:

 <?xml version="1.0"?>
 <config>
     <modules>
        <Jiri_CustomBlock>
            <active>true</active>
            <codePool>local</codePool>
        </Jiri_CustomBlock>
     </modules>
 </config>

Then a file called app/code/local/Jiri/CustomBlock/etc/ config.xml

 <?xml version="1.0"?>
 <config>
     <modules>
         <Jiri_CustomBlock>
             <version>0.0.1</version>
         </Jiri_CustomBlock>
     </modules>
 </config>

This works, I can go to System>Configuration>Advanced and see it inside the Disabled Modules Output

Then I have this in app/code/local/Jiri/CustomBlock/Block/ Header.php

 class Jiri_CustomBlock_Block_Header extends Mage_Page_Block_Html_Header{
      public function helloWorld(){
          return 'Hello World';
      }
 }

Lastly I have this in my custom theme folder in app/design/frontend/customtheme/default/template/page/html/ customblock.phtml

 <?php echo $this->helloWorld(); ?>

Now for the million dollar question; how do I make it so this custom local module/block becomes a child block of the header block?

I have this thusfar in my local.xml but I have no idea how to connect the code to this part.

    <reference name="header">
        <block type="(what do I add here?)" template="page/html_header">
        </block>
    </reference> -->

I looked around online and I found out I can add 'company/module' in the 'type' attribute, but then how does it know to load Block/Header.php?

  • Is that the complete contents of config.xml? – pspahn Jun 17 '15 at 17:22
1

Apply the below changes to your code,

In config.xml

 <?xml version="1.0"?>
     <config>
         <modules>
             <Jiri_CustomBlock>
                 <version>0.0.1</version>
             </Jiri_CustomBlock>
         </modules>
        <global>
          <blocks>
             <jiri_customblock>
                <class>Jiri_CustomBlock_Header</class>
              </jiri_customblock>
          </blocks>
        </global>
     </config>

In local.xml,

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="header">
            <block type="jiri_customblock/header" name="customBlock" as="customBlock" template="page/html/customblock.phtml"/>
        </reference>
    </default>
</layout>
0

in etc/config.xml add block in <global> tag

<?xml version="1.0"?>
 <config>
     <modules>
         <Jiri_CustomBlock>
             <version>0.0.1</version>
         </Jiri_CustomBlock>
     </modules>
<global>
<blocks>
      <jiri_customblock>
        <class>Jiri_CustomBlock_Block</class>
      </jiri_customblock>
    </blocks>

</global>
 </config>

in local.xml add name of block

<reference name="header">
    <block type="jiri_customblock/header" name="html_header" template="page/html_header" >
            </block>
</reference>

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.