Magento get which layout being used on phtml files - magento

Is there a way I could get which layout being used on a certain phtml files?
Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.
I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread
Magento get layout for given page
which stated the use of this snippet
$left_block = $this->loadLayout()->getLayout()->getBlock('left');
but when I tried it on the phtml files, I got an exception error
UPDATE
joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.
In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.
Is there any way I could do that??

If I read the question correctly, then try:
$this->getLayout()->getBlock('root')->getTemplate();

Remove loadLayout():
$left_block = $this->getLayout()->getBlock('left');
By the time you are in the PHTML file, the layout is already loaded.
In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

Related

Calling Models in Magento Templates

I am currently working on integrating HTML cuts into Magento's template, however, I am just a little stumped on the structure of Magento itself. I want to list all of the categories inside a custom template in the 'navigation/left'.phtml file. The following accepted answer Magento: Display sub-category list seems to do what I need to do, however,I don't feel comfortable in calling a model inside of a view files as in MVC, which the accepted answer has done.
Is there a better way of putting this in another section of Magento, or perhaps a custom block which extends the Block_Catalog_Product_List class would be a better way of retrieving the categories?
Thanks
The simplest way to do it is to create a module with a helper inside it, that returns the data you need. Then in the template file call this:
$data = Mage::helper('myhelper')->getCategoryList();
//do your magic with $data
There is no point in overriding blocks unless there is no other solution.

Set template in layout handle for Adminhtml

In the backend of my Magento Webshop I want to set a template for the blocks of System->Config. Therefore I use the layout handle of the Mage_Adminhtml_System_ConfigController which is adminhtml_system_config_index I think. The problem is, nothing happens. Even if want to remove blocks, nothing happens:
<adminhtml_system_config_index>
<reference name="left">
<action method="setTemplate">
<template>myModule/template.phtml</template>
</action>
</reference>
</adminhtml_system_config_index>
OR removing block:
<adminhtml_system_config_index>
<remove name="left" />
</adminhtml_system_config_index>
What am I doing wrong? I even tried system_config_index as layout handle.
You're doing three possible things wrong here.
First Tip: Are you editing the right XML file?
A lot of common layout problems come from the XML file you're editing not being the XML file that's loaded by the system. Switch on developer mode in your site, set display_errors to 1, and then deliberately introduce a non well formed error into your XML files and try to load the page.
<!-- note the trailing greater than sign at the end -->
<adminhtml_system_config_index>
...
</adminhtml_system_config_index> >
If the page loads without error, then you're not loading the XML file you think you are.
Second Tip: Are you using the correct layout handle?
I'm not sure you are. Despite the fact the default system configuration page uses the index action, this action actually forwards the page to the edit action.
#File: app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php
public function indexAction()
{
$this->_forward('edit');
}
Forwarding means there's an internal redispatch, but no http redirect. That means your actual handle is
`adminhtml_system_config_edit`
This is why I use (disclaimer: and built, and sell) Commerce Bug. The Layout tab always displays the current handles, and I can avoid making assumptions that send me down the wrong path.
Third Tip: Are you confusing the block named left with the left hand column?
In general, Magento's container blocks (left, content, right, etc.) are not template blocks. Instead they're container blocks (text/list blocks to be precise) that hold and multiple template blocks.
This is another place I use Commerce Bug, specifically the new Graphviz feature. This will show you the block structure for any particular page. In a factory default system, you'd see something like this for the System Configuration page
As you can see, the left block has no template, so changing its template will have no effect. The block you probably want is left.child1.
Fourth Tip: Is the block you're trying to modify added by the layout XML.
Without getting too deeply into layout rendering (which would take a book), there are some block which are added after the layout generates all its blocks. If this is the case, your block will not be available in layout XML files.
If you look at the editAction method, you can see that the tabs block is added after loadLayout is called (look for adminhtml/system_config_tabs). This means it won't be available in layout xml files.
public function editAction()
{
//...
//the `loadLayout` method call creates blocks based on layout XML files
$this->loadLayout();
$this->_setActiveMenu('system/config');
$this->getLayout()->getBlock('menu')->setAdditionalCacheKeyInfo(array($current));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'),
$this->getUrl('*/system'));
//this line add the `child1` block to the layout. (child one is the
//name chosen since the block has no explicit name
$this->getLayout()->getBlock('left')
->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
//...
This means you can't modify the template of this block from a layout XML file. You'll need to use a custom module that listens for an appropriate event and then makes your changes via PHP. I'd try the event controller_action_layout_render_before or controller_action_layout_render_before_adminhtml_system_config_edit with PHP code that looks something like
$block = Mage::getSingleton('core/layout')->getBlock('child1');
if($block)
{
$block->setTemplate('foo.phtml');
}

How to call a custom php file on magento product page

if getChildHtml('product_type_data') ?> maps directly to catalog/product/view/type/simple.phtml by default, how do I map to my own file? If I wanted to create a file that would produce a small image to place on the product page, right under "availability" how would I tell magento to map to where I have put the file? From what I understand getChildHtml('product_type_data') ?> defaults to the file path: catalog/product/view/type/simple.phtml so how can I customize the magento defaults and tell it to map to my custom files i've created?
Could I do something like getChildHtml('etc/etc/my-file.phtml') ?>
Essentially, what I am trying to do is add a small image under "availability" of my site (ex: http://climbhigh.com/climbing/climbing-ropes/petzl-dragonfly-rope-8-2mm.html) that says free shipping. Just trying to find the best way to do it.
I hope I have explained this well enough, if not, please let me know and I will try to explain more. Any help or guidance would be awesome. Thanks.
The code getChildHtml('product_type_data') doesn't always map directly to the template file catalog/product/view/type/simple.phtml. It only maps to that file if the layout handle PRODUCT_TYPE_simple is loaded, i.e. if the current product is a simple product. In order to change the template to be a different one you need to update the template attribute in the layout. At it's most simple this can be achieved by editing app/design/frontend/base/layout/catalog.xml and changing the template attribute.
<block type="catalog/product_view_type_simple" name="product.info.simple" as="product_type_data" template="your/new/path.phtml">
Of course editing core files is a bad idea, so you should make a quick search for how to correctly add layout updates via either local.xml or customer layout update files.
I ended up figuring out what I needed to do. I simply wanted to add a small image to my product detail page that highlight a free shipping option. All I had to do was create a static block in the admin panel of magento and go into catalog>product>view.phtml file and insert:
getLayout()->createBlock('cms/block')->setBlockId('your_block_id')->toHtml(); ?>
it worked like a charm!
thanks for the help Crags

Adding pager to custom collection block

Sorry for the redundancy of this question, but none of the example and solutions have worked for me. I have a custom module that extends Mage_Catalog_Block_Product_List. The only function in it is _getProductCollection which sets the product collection based on which attribute you want to filter on. It's pretty simple and there are no layout updates involved. I put the block on the page by putting the following into the Content section of a CMS page:
{{block type="vps_featured/list" name="vps_featured_list" attribute_name="best_sellers" template="catalog/product/sale_list.phtml"}}
Since it extends Mage_Catalog_Block_Product_List, I get all the bells and whistles for a product list page, including the toolbar. However, the pager isn't there. I've tried a number of different suggestions but none have helped.
I looked in Toolbar.php in the core code and found where it calls getPagerHtml. I put some debug statements in there and determined that it calls this function when putting my custom block on the page but $this->getChild('product_list_toolbar_pager') is not returning the block, presumably because it isn't there. I tried adding the block using a layout update XML file and adding the <frontend><layout><updates>... tags to my config, but this didn't seem to do anything.
I have Alan Storm's CommerceBug extension, so I pulled that up and exported the layout XML for the page. It doesn't contain any toolbar block at all, despite the fact that the toolbar is on the page and only the pager isn't working. If I do the same thing on a category landing page, it shows the toolbar block in the layout xml. So I've clearly missed something here, but I'm at a loss as to what. I hope someone can help :)
Brian
As it turns out, the solution found HERE works great! You have to add the block using the Layout Update XML rather than adding it in the content section. I still don't quite understand why, but it works.
There already was question about Getting pager to show on magento list

Magento Module action on ProductView

I have create a module which at this point does nothing but exist the next step I need to figure out is how to make it does something when someone views the a product at this point I don't care if it says hello world next to the image
can someone help me I can't seem to figure out what I need to extend if i need to use and observer or what I seem to be lost .... or drowning
Depending on what you want to accomplish, you could go in many different directions from here. If you're looking for visual feedback, creating a new block and adding it to the product page might be a good direction. Try creating a new block in your module (Yournamespace_Yourmodule_Block_Product_View in the file app/code/local/Yournamespace/Yourmodule/Block/Product/View.php) and define a method toHtml in that block that echos some HTML (say "hello world"). Look at other blocks in the system to see how to set up such a class (what to descend from, etc). Later on, you'll want to turn this into a proper template, but this approach will help you understand blocks. Check Alan's other tutorials for how to set up your config.xml to define where blocks are found.
Now, inside your theme, in /templates/catalog/product/view.php, create an instance of your block and display it's contents like this:
<?php print $this->getLayout()->createBlock("yourmodule/product_view")->toHtml(); ?>
This should echo some HTML onto the product view page.
I want to emphasize that this skips several of the steps to doing it the "right" way, but it should get you quick visual feedback and help you understand how a page is built inside of magento.

Resources