0

I am trying to decide the best method of creating two or more template layouts for the template I am working on.

I need to be able to change a number of blocks between the templates along with keeping some default elements across the templates.

For example I want to remove the standard product image gallery on one of the layouts and include a larger main product image whilst the others will retain a smaller product image and the 'more views' gallery.

Whats the best way to accomplish this?

Many thanks

1 Answer 1

1

I like this use case, as it gives a chance to delineate the sometimes miasmatic space that is "view layout in Magento".

When customizing existing layouts I've found it best to involve as few files as possible. Often it's not necessary to change a template file in order to replace one block with another; ultimately it comes down to the markup.

Let's take the example of the main product template remaining suitable for both situations; you just need to change the media template or the template and its block instance. I would start with a simple layout update handle defined in your local.xml layout file:

<PRODUCT_MEDIA_LARGE>
    <!-- if you want same functionality but a different template -->
    <action method="setTemplate" block="product.info.media"><tpl>alt/template.phtml</tpl></action>

    <!-- EDIT: add skin JS via this handle -->
    <action method="addItem" block="head">
        <type>skin_js</type>
        <file>your/css.js</file>
    </action>

    <!-- if you want to displace the original media instance & template -->
    <reference name="product.info">
        <block name="larger.media.block" as="media" type="..." template="..." />
    </reference>
    <!--
        This works using the alias of the media block in the context of its parent.
        The original media block instance still exists if you were to need it.
    -->
</PRODUCT_MEDIA_LARGE>

Then for each product which should have the alternate template, simply enter this in the layout update field*:

<update handle="PRODUCT_MEDIA_LARGE" />

I like this overall approach for two reasons: your layout logic is defined in one place (DRY), and the instructions to apply it are associated with each entity in the database. Each product page loads an entity-specific handle e.g. PRODUCT_8, but I bristle at using arbitrary auto-increment values outside of the database itself.

NB: I'm pretty sure this will work. I have seen some wonkiness when using update handles in the DB-based layout update context, but this use case should be okay.

2
  • Thanks I will give that a go and see if that solves that part of my problem. With the templates I am trying to bring in I need be able to change more than just the media element. For example I want to be able to change the way the product options are displayed (both through CSS but I also want to include other product related details) as well as changing a couple of blocks pulled in through CMS Static Blocks.
    – user6233
    Commented Apr 20, 2014 at 12:02
  • Added a bit regarding the JS.
    – benmarks
    Commented Apr 20, 2014 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.