1

I am trying to add multiple classes to body tag in magento using addbodyclass method in my local.xml file.

Code in my local.xml file

<reference name="root">
     <action method="addBodyClass">
        <className helper="mymodule/addbodyclass/addClasses" />
     </action>
</reference>

Code in my Helper class in a custom module:

class Mycompany_Mymodule_Helper_Addbodyclass extends Mage_Core_Helper_Abstract
{
    public function addClasses() {
        return "classname1 classname2 classname3";
     }
}

This adds classes in one string like this:

classname1-classname2-classname3

in the body tag.

Instead of spaces it adds dash(-). I know this is default behavior of magento. But is there a way to actually add spaces and not dash(-).

Thank You

2 Answers 2

0

So basically the addBodyClass method replaces some characters with - on the fly using regular expression:

public function addBodyClass($className)
{
    $className = preg_replace('#[^a-z0-9]+#', '-', strtolower($className));
    $this->setBodyClass($this->getBodyClass() . ' ' . $className);
    return $this;
}

As you can see, when this method is called the original body classes are updated and the extra body class is added with a space between the original classes. So in order to add several classes I suggest you do the following in your xml file:

<action method="addBodyClass">
     <className>classname1</className>
</action>
<action method="addBodyClass">
     <className>classname2</className>
</action>
<action method="addBodyClass">
     <className>classname3</className>
</action>

If you really want to be able to add several classes to the body using that method, then you will need to create a custom module that extends that method:

config.xml

<?xml version="1.0"?>

<config>

    <modules>
        <DigitalPianism_Multiplebodyclass>
            <version>0.0.1</version>
        </DigitalPianism_Multiplebodyclass>
    </modules>
    <global>
         <blocks>
              <page>
                   <rewrite>
                        <html>DigitalPianism_Multiplebodyclass_Block_Page_Html</html>
                   </rewrite>
              </page>
         </blocks>
    </global>

</config>

Block/Page/Html.php

<?php

class DigitalPianism_Multiplebodyclass_Block_Page_Html extends Mage_Page_Block_Html
{
    public function addBodyClass($classNames)
    {
        if (is_array($classNames))
        {
            foreach($classNames as $className)
            {
                parent::addBodyClass($className);
            }
        }
        else parent::addBodyClass($classNames);
        return $this;
    }
}

Then in your custom helper you will be able to do:

public function addClasses() {
        return array('classname1','classname2','classname3');
     }
3
  • I know this way also about adding classes in xml file one by one. But this is a tedious job to maintain specially when you have classnames generated dynamically. So i chose to go with helper method addClasses.
    – Jay Babani
    Commented Mar 1, 2016 at 14:22
  • @JayBabani see my edited answer for further possibilities Commented Mar 1, 2016 at 14:32
  • Perfect Solution. I think magento team guys should add this code in their core files. Thank you
    – Jay Babani
    Commented Mar 2, 2016 at 7:37
1

Out of the box, you cannot do that because the addBodyClass looks like this:

public function addBodyClass($className)
{
    $className = preg_replace('#[^a-z0-9]+#', '-', strtolower($className));
    $this->setBodyClass($this->getBodyClass() . ' ' . $className);
    return $this;
}

It replaces anything that's not alphanumeric with -.

The solutions I see are (in the order of easiness to implement):

  1. You add a tag <action method="addBodyClass"> to the layout for each class you need. This may not always work if you want to add a variable number of classes.
  2. rewrite the method Mage_Page_Block_Html::addBodyClass to allow you to use spaces in the class names.
  3. Create an other block that does notout put html but calls addBodyClass method as many times as you need.
    Something like this.

    <reference name="root">
         <block type="[module]/some_name">
             <action method="addRequiredBodyClasses" />
         </block>
    </reference>
    

    then create the block [Namespace]_[Module]_Block_Some_Name.

    <?php 
    class [Namespace]_[Module]_Block_Some_Name extends Mage_Core_Block_Abstract
    {
        protected function _toHtml()
        {
            return '';
        }
        public function addRequiredBodyClasses()
        {
            $bodyClasses = Mage::helper('mymodule/addbodyclass')->addClasses();
            $parentBlock = $this->getParentBlock();//this will get you the root block;
            if ($parentBlock instanceof Mage_Page_Block_Html)
            {
                 $bodyClassesArray = explode(' ', $bodyClasses);
                 foreach ($bodyClassesArray as $class) {
                     $parentBlock->addBodyClass($class);
                 }
            }
        }
    }
    
1
  • this looks interesting. I will try this. Thank you so much
    – Jay Babani
    Commented Mar 1, 2016 at 14:31

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.