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.
-
share your block and XML where you want to reuseVishwas Bhatnagar– Vishwas Bhatnagar2016年11月25日 10:30:13 +00:00Commented Nov 25, 2016 at 10:30
2 Answers 2
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>
-
This is the exact answer to the question.The Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:21:47 +00:00Commented Nov 25, 2016 at 11:21
-
And what does the after=-= mean?The Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:22:12 +00:00Commented 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>The Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:26:59 +00:00Commented 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 fileFme Extensions– Fme Extensions2016年11月25日 11:30:27 +00:00Commented 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>The Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:32:25 +00:00Commented Nov 25, 2016 at 11:32
Solved it partially by using methods of the block in template. I just:
- Created the block
- Declared the block in module's config.xml
- Using it in the template (a .phtml file) like so:
echo $this -> getLayout() -> createBlock('mymodule/myblock') -> toHtml();
I want to do it just with .XML layout updates though.
-
which url you trying to set your template?Fme Extensions– Fme Extensions2016年11月25日 11:02:44 +00:00Commented Nov 25, 2016 at 11:02
-
This url: 127.0.0.1/mysite/firstcategory.htmlThe Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:03:30 +00:00Commented Nov 25, 2016 at 11:03
-
you want to set template for
category page?Fme Extensions– Fme Extensions2016年11月25日 11:06:06 +00:00Commented 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.The Outstanding Question Asker– The Outstanding Question Asker2016年11月25日 11:07:07 +00:00Commented Nov 25, 2016 at 11:07