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.

Himanshu
1,76618 silver badges34 bronze badges
asked Nov 25, 2016 at 10:23
1
  • share your block and XML where you want to reuse Commented 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>
answered Nov 25, 2016 at 11:12
17
  • This is the exact answer to the question. Commented Nov 25, 2016 at 11:21
  • And what does the after=-= mean? Commented 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> Commented 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 Commented 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> Commented 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.

answered Nov 25, 2016 at 10:33
4
  • which url you trying to set your template? Commented Nov 25, 2016 at 11:02
  • This url: 127.0.0.1/mysite/firstcategory.html Commented Nov 25, 2016 at 11:03
  • you want to set template for category page? Commented Nov 25, 2016 at 11:06
  • I don't want to change the template file template/catalog/category/view.phtml. I would like to add block to the template and layout by using XML. Commented Nov 25, 2016 at 11:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.