Magento block per action - magento

I am trying to create a custom collection page with pagination. I have created the code for the pagination in the block and can output it in the template.
However the module that I am creating has pages that have other templates with no pagination.
How it works: User goes to index.php/styles/choose/items and selects the attributes/categories of the products he wants to display. He clicks on submit and is redirected to index.php/styles/choose/products where he can see the products and pagination.
In my styles.xml I have
<styles_choose_items>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="styles/styles" name="styles" template="styles/styles.phtml"/>
</reference>
</styles_choose_items>
<styles_choose_products>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="styles/products" name="products" template="styles/products.phtml"/>
</reference>
</styles_choose_products>
In config.xml I have :
<blocks>
<styles>
<rewrite>
<styles>Nuvo_Styles_Block_Styles</styles>
</rewrite>
<rewrite>
<products>Nuvo_Styles_Block_Products</products>
</rewrite>
</styles>
</blocks>
In the controller :
public function itemsAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function productsAction()
{
$this->loadLayout();
$this->renderLayout();
}
I would really like to know what I am doing wrong. The index.php/styles/choose/items page displays correctly, however the index.php/styles/choose/products show only the template and if I try to add anything to the Products.php block it goes blank with no error.
Any help is appreciated.
Thank you!

Thank you for your feedback. I've enabled line 77 in index.php ini_set('display_errors', 1); to see the errors.
I was actually very close. There was just an error in the Products.php block file. It now works correctly.

the declaration of blocks in your config is wrong, you are not rewriting blocks, as far as I can tell. Try this:
<blocks>
<styles>
<class>Nuvo_Styles_Block</class>
</styles>
</blocks>
Also it would be useful to see Nuvo_Styles_Block_Products

Related

how to remove categories from left column in customer account page in magento?

I don't want to show categories in left column in customer account page.I have tried but didnot find the solution to solve this problem.I want any condition to remove categories only from customer acoount page.
If anyone have any idea,please help me.
Thanks!
You can do this by xml
create a local.xml under app/design/frontend/youpackage/youtemplate/layout/
if you left panel category alias name is "alias_name_of_left" then you can do this by
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<default>
<reference name="left">
<action method="unsetChild"><name>alias_name_of_left</name></action>
</reference>
</default>
</layout>
More Details at http://magentist.com/magento_help/remove-sidebar-blocks-with-layout-update-xml/
In you local.xml file of your theme add as follow,
Path: app/design/frontend/default/mytheme/layout/local.xml
<customer_account handle is used for only customer account pages
<layout>
<customer_account translate="label">
<reference name="left">
<remove name="catalog.leftnav"/>
</reference>
</customer_account>
</layout>
Flush caches and check.
You can remove block form customer.xml also for that first off all copy xml into you current theme. I have define the path.
Path: app/design/frontend/default/mytheme/layout/customer.xml
Under customer_account handle.
<customer_account translate="label">
<reference name="left">
<remove name="catalog.leftnav"/>
</reference>
</customer_account>

What is preventing my layout updates from being displayed

I am trying to display a simple message on my Magento site using a special block that I have created. I have been able to easily unset blocks and insert them in other places on the home page, but I am running into trouble when I try to do the same thing on one of the product pages. I have created a file at app/design/frontend/base/default/layout/packagename/modulename.xml
with the following contents:
<?xml version="1.0"?>
<layout>
<default>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
</default>
</layout>
It seems to me like the code above would remove the search bar from a product page and add a block in the product info section that says "Free shipping!" but when I load the page there are no changes. I have tried using "remove" to alter some of the blocks on the page and it works, so the file is definitely being loaded into the layout.xml. I have also tried making my changes in the local.xml file instead, with the same results. Other than that, I am kind of at a loss for things to try to get this to work correctly.
Edit: To provide some more information on the problem, if I were to replace my changes with something like
<reference name="root">
<action method="unsetChild">
<name>header</name>
</action>
</reference>
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
I think you need to be more specific with your layout handler, you are setting it as and that means all pages, i recommend you change that handler by
<?xml version="1.0"?>
<layout>
<catalog_product_view>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="product.info">
<block name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</block>
</reference>
</catalog_product_view>
</layout>
Greetings.
First of all, never put your stuff in the base/default folder.
On to your question. What if you try it like this:
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
To answer your other question:
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
It's not the fact it's on the root block, it's that you should use unsetChild within the <reference/> part.

Overrule Magento template

I would like all my CMS pages (but not all pages) to use a custom template file, however when I use the setTemplate action in my local.xml file it's not changing the template. The block is rendering correctly but without the correct layout.
The XML I'm using right now is:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<cms_page_view>
<reference name="root">
<action method="setTemplate"><template>page/cms-page.phtml</template></action>
</reference>
<reference name="right">
<block type="catalog/navigation" name="default_page_view" template="navigation/game-menu.phtml"/>
</reference>
</cms_page_view>
</layout>
What am I doing wrong?
You aren't doing anything wrong - your directive is being overridden by the entity data. For the reason why, see Mage_Cms_Helper_Page::_renderPage():
protected function _renderPage(/*...*/)
{
//snip...
$action->getLayout()->getUpdate()
->addHandle('default')
->addHandle('cms_page');
$action->addActionLayoutHandles();
if ($page->getRootTemplate()) {
$handle = ($page->getCustomRootTemplate()
&& $page->getCustomRootTemplate() != 'empty'
&& $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
$action->getLayout()->helper('page/layout')->applyHandle($handle);
}
//snip...
}
So, your directive is being processed under the full action name handle cms_page_view, which is added via the $action->addActionLayoutHandles(); call. Whereas CMS pages are practically always saved via the admin with a root_template value, this value will always override file-based directives.
While it would be possible to update the data, it would be at risk of being overwritten when In order to provide an alternate template which will be preserved when the page is edited via the admin, it's necessary to specify some configuration values and some corresponding layout XML. In your custom module's config XML (or in app/etc/local.xml if this is a non-distributed change):
<global>
<page>
<layouts>
<cms_page_custom>
<label>Empty</label>
<template>page/cms-page.phtml</template>
<layout_handle>cms_page_custom</layout_handle>
</cms_page_custom>
</layouts>
</page>
</global>
This will provide the option to the select input during CMS page administration. To complete this work, in your custom layout XML:
<cms_page_custom>
<reference name="root">
<action method="setTemplate"><template>page/cms-page.phtml</template></action>
<!-- Mark root page block that template is applied -->
<action method="setIsHandle"><applied>1</applied></action>
<action method="setLayoutCode"><name>empty</name></action>
</reference>
</cms_page_custom>

rendering blocks and child blocks within a theme

I am currently trying to get a better understanding of how blocks work in Magento. I have looked at some of the files to get a better idea and it has helped a little, but they are a too to complex for my limited skills at the moment and I still do not have proper understanding of what is going on and how to implement them into my site. I realise they are essential to understand for working with Magento so I thought I would set up a list of things to try and achieve:
display a block (done)
display a block and child block
display a block within a magento layout
position a block on the page of a magento layout
learn the most commonly used 'type' attributes and when to use them
So far I have put together
_index_index
Namespace/Module/etc/config.xml
<frontend>
....
<layout>
<updates>
<learningblocks>
<file>Namespace/Module/childblocks.xml</file>
<file>Namespace/Module/blocks.xml</file>
</learningblocks>
</updates>
</layout>
</frontend>
Namespace/Module/controllers/IndexController.php
class Namespace_Module_IndexController
extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout('learningblocks')->renderLayout();
}
public function blocksAction()
{
$this->loadLayout('blocknode')->renderLayout();
}
}
frontend/base/default/layout/namespace/module/blocks.xml
<layout>
<blocknode>
<block type="core/text" name="blocktest" output="toHtml" >
<action method="setText">
<args>some text to display on screen</args>
</action>
</block>
</blocknode>
</layout>
The above worked as expected and displayed the string 'some text to display on screen' on a white page. But thats all i've been able to do, I cannot get child blocks to render onto the screen and I cannot display anything within a theme, let alone try and move it about within that theme
Below is one of my attempts that I cant seem to get to work. Why is this not working?
frontend/base/default/layout/namespace/module/childblocks.xml
<layout>
<abcde>
<block type="core/template" name="childblocks" output="toHtml" template="namespace/module/childblocks.phtml">
<block type="core/text" name="anyname">
<action method="setText">
<args>Some text to add to this page</args>
</action>
</block>
</block>
</abcde>
<learningblocks_index_index>
<update handle="abcde" />
</learningblocks_index_index>
</layout>
frontend/base/default/template/namespace/module/childblocks.phtml
<p>from the childblock.phtml page</p><?php $this->getChildHtml(); ?>
NB: I have changed the namespaces and module names to be more generic, in the hope it is easier to read (they wern't very well chosen names).
I know this is not a complete answer, but it may help those who have struggled with the same problem. I have not gone into great depth as I assume if your searching for an answer you will have already read THIS ARTICLE and that covers it all, I assume you have the same issue as I did i.e. a misunderstanding of what you had learnt from this tutorial.
On reading this answer please be aware I am very new to Magento and there could be some inaccuracies in here, if there are I am sure someone will correct me and edit accordingly.
Firstly this is wrong
public function indexAction()
{
$this->loadLayout('learningblocks')->renderLayout();
}
It should be this
public function indexAction()
{
$this->loadLayout()->renderLayout();
}
and then you will have to map learningblocks node in layout xml to that action module_controller_action. Doing this will display the block in the page within your theme.
So to render a child block
Add something like this in you layout.xml
<module_controller_action>
<reference name="content">
<block type="module/blockname" name="unique_name" output="toHtml" template="path/toyou/template.phtml" >
<block type="module/blockname" name="another_unique_name" output="toHtml" template="path/toyou/template.phtml" />
</block>
</reference>
</module_controller_action>
then in your template file echo out
$this->getChildHtml('another_unique_name')
If you want to remove blocks from your page use the remove node such as
<remove name="right"/>
<remove name="left"/>
This page will offer a list of attributes that can be used to be honest I found that looking through the magento files helped more than that page though

Magento template not loaded properly

I created a new module and I try to load a template in an action under indexcontroller. That template loaded properly but the basic/root templates are not loaded. Magento loaded only the template file what I specified in the XML. I explained the steps below what I followed.
Create a module named 'Sample'.
Create a IndexController with a index action.
Create local.xml file under the dir app/design/frontend/default/default/layout/
Create sample_page.phtml under the dir app/design/frontend/default/default/template/sample/sample_page.phtml.
IndexController:
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
local.xml:
<layout version="0.1.0">
<default>
</default>
<sample_index_index>
<reference name="root">
<block type="page/html" name="root" output="toHtml" template="sample/sample_page.phtml">
</block>
</reference>
</sample_index_index>
Could anybody find the issue what I done?
The issue here is that you referenced the wrong layout block ("root" in your example). Doing that replace the entire set of blocks for page by the one you specify.
To get all the blocks displayed (included yours), simply reference another layout block than "root", for example "content".
You could also want to modify the root layout block for pages of your module, if that is the case, in the default handle, reference the root block and set a different template.
Here is an example :
<layout version="0.1.0">
<default>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
</default>
<sample_index_index>
<reference name="content">
<block type="page/html" name="the_name_you_like" output="toHtml" template="sample/sample_page.phtml">
</block>
</reference>
</sample_index_index>
</layout>

Resources