Magento layout related basic Questions? - magento

Here is what I think about Magento (plz correct me if I am wrong )
1)Each module has its own layout.xml stored in /interface/theme/layouts/ folder.
2)Magento loads all these layouts for current theme and creates a big xml file.
Now what I am confused at .
a)If magento loads /default/default/ (interface & theme) then why all the templates & layouts are inside base/default/ ??
b)what if I create my module name “page” inside my namespace “Jason” i.e Jason_Page , now what will happen to blocks in layout files which are named
c)Since all the layouts are loaded and merged into one big xml file , then what happen to all those reference blocks which have same name attribute and are inside “Default” handle tag ?
e.g
d)what is Local.xml layout for and its use ??
e)wats the relation ship between a module name foo , and its layout name foo.xml ?
What will happen to layout.xml if two modules with same name exist in diff namespace ?
Thanks in advance .

1) Each module can choose to define layout files that go into the /interface/theme/layouts/ folder. You can accomplish this by specifying the layouts in your module's config.xml file like this:
<frontend>
<layout>
<updates>
<jason_page>
<file>jasonpage.xml</file>
</jason_page>
</updates>
</layout>
</frontend>
2) Yes.
a) Magento's interface is built using descendency. Templates and layouts are first taken from your chosen interface and theme, and if not found there, will be taken from base. This allows you to define only those things that change from the base when creating a new them.
b) If you create a new module with blocks, you will specify the classpath for those blocks in your config.xml:
<global>
<blocks>
<jason_page>
<class>Jason_Page_Block</class>
</jason_page>
</blocks>
</global>
This will define the tag jason_page to refer to your blocks. Then, when defining a layout, you will reference your blocks as:
<block type='jason_page/blockname' as='blockname' name='blockname' />
And your blocks will be named as: Jason_Page_Block_Blockname in /app/code/local/Jason/Page/Block/Blockname.php
c) Each time you use a <reference> tag and add some child blocks, they are added to the large tree as you say. Make sure to use different name/as tags for the blocks, and you'll be fine. If the name is identical, you will probably cause errors.
d) local.xml (in app/etc) defines some configuration parameters for your site, such as database connection information and encryption keys. it doesn't have to do with layouts.
d part 2) Since you define your layout files, including their names, it's up to you to not cause collisions. Choosing a module name more unique than Page would go far here. If you do have a module with the same name as a default module (e.g. Page), prefix the file with your namespace when declaring it in your config.xml file.
Hope that helps!
Thanks,
Joe

Just to add to Joe's answer, you can have interface/theme/layout/local.xml file, and it will be loaded after all other core and module layout XML files.
Use this file for local theme customizations.

I just recently wrote a post about how to use static blocks. Here is the meat of it:
Step One: Create Static Block in Your Magento Admin
Magento Admin Panel—>Static Blocks—>Add New Block
1) Name your Static Block, in this case Custom footer Links
2) Label the Identifier (This is the link you will use to call the block later) in this case, custom-footer-links
3) Choose what store view you would like it to render in
4) Set Status to Enabled
2)Now for the fun part! Add your navigation links to the block. Make sure to use to make them match your sites color and theme.
Step Two: Inserting Code to Call the Static Block
This part is going to require you to FTP into your Magento site and modify footer.phtml
app—>design—>frontend—->default—>(your template)—>template—>page—>footer.phtml
Find where in the footer you want your navigation links to display and insert:
getLayout()->createBlock(‘cms/block’)->setBlockId(‘custom-footer-links’)->toHtml(); ?>
Now most of the time the Static block should display just fine but in some cases you are going to have do some extra steps to have the block display.
1) Instead of inserting:
getLayout()->createBlock(‘cms/block’)->setBlockId(‘custom-footer-links’)->toHtml(); ?>
Use:
custom-footer-links
2)Modify catalog.xml
app—>design—>frontend—>default—>f002—>layout—>catalog.xml
Add under
custom-footer-links

Related

Magento Layout updates for specific module

Lets suppose I want to add a CSS file on a particular Action, its easy with <module_controler_action> and if I use <default> it will apply to every page, but what if my module contains 100 actions (pages) and I want to add a CSS file only to specific module pages, how can I do that without applying to to every page of magento?
please help
to add CSS only for particular module then add your CSS file in the following tag <module_controller>

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');
}

Magento: include custom block in view.phtml

I have created a block accrording to this tutorial: http://www.gravitywell.co.uk/blog/post/how-to-creating-your-own-custom-block-in-magento . Now I need to use my manufacturerblock.phtml on my pruduct view page (just somewhere closed to product description text).
In local.xml (under <reference name="header">) I added: (edit: I tried also under <reference name="product.info"> in <catalog_product_view>
<block type="aimitmanufacturerblock/manufacturerblock" name="manufacturerblock" as="manufacturerblock" template="aimitmanufacturerblock/manufacturerblock.phtml" />
and I used <?php echo $this->getChildHtml('manufacturerblock')?> in my view.phtml.
But it seems the phtml is not included.
Edit: Apologies, I should have followed the link to the tutorial you gave first, I see it's all about creating a new block type! However, I'll leave my answer for the moment - why do you need to create a new block type at all, there are loads of built in block types?. This answer from Clockworkgeek could be useful to you too;
Create a new Block in Magento
Original answer;
The issue is your block type - the block type isn't like a variable that you define yourself, there are specific built-in Magento block types which are used to instantiate the appropriate classes. This SO thread will be useful to you for further explanation;
Understanding Magento Block and Block Type
For another great explanation on block types read this one too;
What Block Type for Left Column in Magento Theme?
If you want to use the block in catalog/product/view.phtml, you need to reference product.info in the catalog_product_view handle.
And I can just assume that the module is activated, the class alias is declared, the class is created and the cache is flushed

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

Magento - conditionals in layout files

Is it possible in Magento to conditionally add blocks into a layout xml file?
Im thinking along the lines of having an admin config option checkbox - if checked then a block needs to be added to the page and vice versa if not checked.
I could think of a way of doing this via code but not the actualy layout file system itself.
The ifconfig paramater can be used to conditionally call an action method
<action method="someBlockMethod" ifconfig="path/to/config"><param1>value</param></action>
The path/to/config path is passed to Mage::getStoreConfigFlag() to return a boolean.
I'd try using this in combination with the insert method
<action method="insert" ifconfig="path/to/config"><param>block_name</param></action>
The block with the name or alias of block_name will need to be already inserted into the layout object by other PHP or XML, so you may need to take additional steps to unset it from its original blocks after inserting it into your new block.
you can try this (i haven't tried it myself):
<action ifconfig='your/extension/active'

Resources