The article is dedicated to some questions from Magento 1 front end developer certification. The FAQs are written in a simple and clear way by the developers who have already passed the exam. Hope it’ll help you improve your skills and solidify your knowledge.
Understanding processing order of layout handles and other directives
Using XML, you’re able to perform any action: add, remove and modify blocks. But first, it’s necessary to know the structural blocks that are commonly used. These are header, left, content, right, footer. At the same time, you need to be good at layout handles the structure of which looks as follows: [module_front_name]_[controller_name]_[action_name], that means if we want to add a block on the main page, we will have to include cms_index_index too.
Let’s have a closer look at the example how to add a block via XML on the main page. XML will be like that:
1 2 3 4 5 |
<cms_index_index> <reference name="content"> <block type="core/template" name="index-banner" template="banner-index.phtml" /> </reference> </cms_index_index> |
If we want to remove a block with a banner, so the code will be as follows:
1 2 3 |
<reference name="content"> <remove name="index-banner" /> </reference> |
Specify the root template for all pages or for a specific page
In order to specify the root template for all pages, we need to open XML pages, for example, page.xml and add in <default>:
1 2 3 4 5 |
<reference name="root"> <reference name="root"> <action method="setTemplate"><template>page/2columns-left.phtml</template></action> </reference> </reference> |
If you want to specify the root template for a specific page, it’s necessary to write its handles by type [module_front_name]_[controller_name]_[action_name], for example, the contact page:
1 2 3 4 5 |
<contacts_index_index> <reference name="root"> <action method="setTemplate"><template>page/3columns.phtml</template></action> </reference> </contacts_index_index> |
Assign a customized template file using layout XML
Here we need to use the setTemplate method. Let’s consider the following example. In order to use a customized template on the product page view-custom.phtml located in the folder with a pathway /template/catalog/product/view-custom.phtml, we can add the following lines into local.xml files of our theme:
1 2 3 4 5 6 7 |
<catalog_product_view> <reference name="product.info"> <action method="setTemplate"> <template>catalog/product/view-custom.phtml</template> </action> </reference> </catalog_product_view> |