Magento: show custom attribute in body class -
i have custom category attribute want add body class. far find out people is
override categorycontroller , add
$root->addbodyclass($category->getmyattribute());don't want override core classes...in admin panel add
<reference name=”root”><action method=”addbodyclass”><classname>caravan-motorhome-lighting</classname></action></reference>each , every category not using attribute adding class directly. have attribute, surely don't want clone , add class way.
so favourite solution layout update can add local.xml says
<reference name=”root”> <action method=”addbodyclass”> <classname> value of custom attribute here dynamically </classname> </action> </reference> does have idea how work or idea didn't think of?
you can use cool feature of magento layout xml achieve this. you'll need module achieve it. either create module or use theme module if have 1 — decide think best.
i'll show example i'll add class containing id of category body tag:
in layout xml, i'm going add via catalog_category_default handle. way, can use mage::registry('current_category') later retrieve current category. so, in layout xml similar this:
<catalog_category_default> <reference name="root"> <action method="addbodyclass"> <classname helper="mymodule/my_helper/getcategoryclass" /> </action> </reference> </catalog_category_default> this attribute important part: helper="mymodule/my_helper/getcategoryclass". equivalent calling mage::helper('mymodule/my_helper')->getcategoryclass(); in code.
whatever returned function used value <classname> node. may want use different helper deem more appropriate, decide.
carrying on example, here's function:
public function getcategoryclass() { return 'category-id-' . mage::registry('current_category')->getid(); } you'll want change code retrieves value of attribute. e.g getmyattribute() on category returned mage::registry('current_category').
also, you'll need ensure return suitable css class. in example don't need id number appended category-id-. if value of attribute not going safe might want consider using like this

Comments
Post a Comment