1

How can I add a block through XML layout file, and not programaticaly, through controller?

I have my block and I want to use it in a template that already exists (catalog/category/view.phtml).

How can I do this without going in and changing a controller, and without changing the catalog/category/view.phtml directly?

I want to add this block somewhere in an already existing template through XML, and not programatically.

This is part of my block:

class Mynamespace_Mymodule_Block_Myblock extends Mage_Core_Block_Template
{
    public function _construct()
    {
        $this -> setTemplate("mynamespace_mymodule/myblock.phtml");
        return parent::_construct();

    }

    public function getItemsByCategoryAndUseState($category, $use_state) {
        return "some used or new items";
    }
}

This is config.xml of my module:

<config>
<modules>
    <Mynamespace_Mymodule>
        <version>3.0.0.1</version>
    </Mynamespace_Mymodule>
</modules>
<global>
    <blocks>
        <mynamespace_mymodule>
            <class>Mynamespace_Mymodule_Block</class>
        </mynamespace_mymodule>
    </blocks>
</global>
<frontend>
    <layout>
        <updates>
            <mynamespace_mymodule>
                <file>mynamespace_mymodule.xml</file>
            </mynamespace_mymodule>
        </updates>
    </layout>

</frontend>

This is my package/theme/layout/mynamespace_mymodule.xml:

<layout version="0.1.0">
    <catalog_category_view> 
        <reference name="product_list"> 
            <block type="mynamespace_mymodule/myblock" name="myblock" after="-" /> 
        </reference> 
    </catalog_category_view>
</layout> 

Thanks.

1
  • share your block and XML where you want to reuse Nov 25, 2016 at 10:30

2 Answers 2

1

you can add block with this xml code

<catalog_category_view>
    <reference name="content">
        <block type="mymodule/myblock"  after="-" />
     </reference>
</catalog_category_view>

add this is <frontend> tag not in <global>

     <frontend>
      <layout>
        <updates>
            <mynamespace_mymodule>
                <file>mynamespace_mymodule.xml</file>
            </mynamespace_mymodule>
        </updates>
      </layout>
    </frontend>
17
  • This is the exact answer to the question. Nov 25, 2016 at 11:21
  • And what does the after=-= mean? Nov 25, 2016 at 11:22
  • Something is wrong. I am not getting the output from the block when I added this in config.xml of the module: <frontend> ... <layout> <updates> <catalog_category_view> <reference name="content"> <block type="mynamespace_mymodule/myblock" name="useditems" after="-" /> </reference> </catalog_category_view> </updates> </layout> </frontend> ... </config> Nov 25, 2016 at 11:26
  • @VladimirDespotovic which file your are editing if it is layout file simple add this code <catalog_category_view> <reference name="content"> <block type="thinkopen_overwriter/filteredbooks" name="usedbooksincategory" after="-" /> </reference> </catalog_category_view> layout file is inapp/design/frontend/spacename/themename/layout/thier are layout file Nov 25, 2016 at 11:30
  • See my comment above. It didn't work. I added this in my design/frontend/package/theme/layout/mynamespace_mymodule.xml: <layout version="0.1.0"> <frontend> <layout> <updates> <catalog_category_view> <reference name="content"> <block type="mynamespace_mymodule/myblock" name="nameofmyblock" after="-" /> </reference> </catalog_category_view> </updates> </layout> </frontend> </layout> Nov 25, 2016 at 11:32
0

Solved it partially by using methods of the block in template. I just:

  1. Created the block
  2. Declared the block in module's config.xml
  3. Using it in the template (a .phtml file) like so:
  4. echo $this -> getLayout() 
        -> createBlock('mymodule/myblock') 
        -> toHtml();
    

I want to do it just with .XML layout updates though.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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