6

While studying for the Frontend Certification, I noticed a new thing I never came accross.

Basically when I want to use the <action> tag, let's say to change the template of a block, I'm used to do the following:

<reference name="block.name">
    <action method="setTemplate">
        <template>my/template.phtml</template>
    </action>
</reference>

But today, I noticed you could also do it like this:

<action block="block.name" method="setTemplate">
    <template>my/template.phtml</template>
</action>

It works fine but I've never came accross such code in the core.

  • Is that valid code or is that "hacky" ?
  • Which syntax would be recommended as best practice ?
11

So this is basically only allow two different ways to do the same thing. The code that actually picks which one to use has the "fallback". It can be found in Mage_Core_Model_Layout::_generateAction

if (!empty($node['block'])) {
    $parentName = (string)$node['block'];
} else {
    $parentName = $parent->getBlockName();
}

To answer your questions:

  1. Is it "hacky" nope it looks to have been designed to do either and in-fact you could put a case forward that the fallback to the parent is the "hacky" option as this is a fallback.
  2. What is best practice. This one is more personal I would say that best practice is to pick one and stick to it. I would not have both in my code, I don't care which one to do but switching in the same project/file would be crazy to me.

One thing to consider when picking what syntax you would like to do is not simply how it looks when there is one but how it looks with many actions. For example let's take the catalog layout app/design/frontend/base/default/layout/catalog.xml.

<catalog_product_compare_index translate="label">
    <label>Catalog Product Compare List</label>
    <!-- Mage_Catalog -->
    <reference name="root">
        <action method="setTemplate"><template>page/popup.phtml</template></action>
    </reference>
    <reference name="head">
        <action method="addJs"><script>scriptaculous/scriptaculous.js</script></action>
        <action method="addJs"><script>varien/product.js</script></action>
        <action method="addJs"><script>varien/product_options.js</script></action>
    </reference>
    <reference name="content">
        <block type="catalog/product_compare_list" name="catalog.compare.list" template="catalog/product/compare/list.phtml"/>
    </reference>
</catalog_product_compare_index>

Now with the above xml I think you can "easily" see what block is being changed in what way.

Now you could change it to look as follows:

<catalog_product_compare_index translate="label">
    <label>Catalog Product Compare List</label>
    <!-- Mage_Catalog -->
    <action method="setTemplate" block="root"><template>page/popup.phtml</template></action>
    <action method="addJs" block="head"><script>scriptaculous/scriptaculous.js</script></action>
    <action method="addJs" block="head"><script>varien/product.js</script></action>
    <action method="addJs" block="head"><script>varien/product_options.js</script></action>
    <reference name="content">
        <block type="catalog/product_compare_list" name="catalog.compare.list" template="catalog/product/compare/list.phtml"/>
    </reference>
</catalog_product_compare_index>

But for me this would become very "unreadable" fairly quickly, saying that deeply nested xml also becomes un-readable quickly too.

  • 1
    Very interesting. Yeah it seems like it has always been there. I like it because it saves two lines of code too. I was wondering why the core team does not use it at all. Thanks for your input – Raphael at Digital Pianism Dec 16 '16 at 10:58
  • 2
    @RaphaelatDigitalPianism probably simply a forgotten feature. The person that wrote that line of code may have moved on before it was "used". Also it might have been considered that the nesting approach looks better consider if you have to do lots of actions to lots of block on the same level it could easily become a mess. – David Manners Dec 16 '16 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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