cssmagentolayout-xml

magento - layout update xml to add remove or modify element class names


I've been trying to figure out some layout update xml directives to add, remove or otherwise modify css attributes for elements. Something like:

<reference name="breadcrumbs">
    <action method="setElementClass"><value>light</value></action>
</reference>

But without any luck at all.

Why do this? - because, in this case and for some pages, I want to handle the breadcrumbs differently. I spose I could make a few different breadcrumb templates and update them on a page by page basis, but this seems like overkill.

Given the Googles silence on this subject makes me wonder if I'm the only one to ever need this seemingly reasonable behavior.

Is there a way to elegantly modify classes for template blocks via update xml?

cheers -

b[]x


Solution

  • You need to understand the purpose of the command you are running,

    <action method="setElementClass">
    

    Which means that on the breadcrumbs block, you are calling the function setElementClass. The set and get functions are a standard call of the Varien Object, it is just a way to set a variable in the class instance.

    So what you are doing above is the equivalent of doing:

    $class->setElementClass('myvalue')
    

    Now, you can set whatever variable you like, but unless the block/phtml file actually use that variable, it isn't going to do anything.

    In order to modify the CSS classes via XML, you'll also need to edit the corresponding .phtml file and include:

    class="<?php echo $this->getElementClass(); ?>"
    

    Wherever you plan on using it. There is nothing wrong with hardcoding CSS classes in the phtml files - that's what they are they for. Using XML to update these values is a bit long winded - UNLESS you are making a white-label store and wish to use a simple XML swap to make changes quick and simple.