0

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

Bosworth99
  • 4,206
  • 5
  • 37
  • 52

2 Answers2

3

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.

Ben Lessani
  • 2,141
  • 14
  • 16
  • Perfect! That was exactly what I was missing... I completely understand that the layout xml is just a wrapper language for calling methods and supplying variables... But still, getting it all lined up is not completely straightforward (and hell - I've only been working in this system for a few days...). Anyway - emitting the element class in the template file did the trick. So many thanks! – Bosworth99 Mar 12 '12 at 22:55
  • ... and while I've got you - are there getters and setters for generic variables? I understand that custom objects could support this, but what about the base varien object? – Bosworth99 Mar 12 '12 at 23:02
  • answered my own question: addData and getData... awesome. – Bosworth99 Mar 13 '12 at 17:02
  • 1
    Have a look at ./lib/Varien/Object.php - you'll find some really cool functions in there that aren't even used in core modules. – Ben Lessani Mar 13 '12 at 18:02
0

I dont think there is a way to modify css classes through xml in Magento. Class names are hardcoded in .phtml files. So i guess the only(actually 2) way to change your styles is either to overwrite .phtml files with your new .phtml files with updated css classes or change stylesheet style definitions for already existing class names in styles.css

Drew Hunter
  • 10,136
  • 2
  • 40
  • 49
Jagadeesh
  • 188
  • 1
  • 3