How to add a new body class to Magento layout

In: Frontend

11 Dec 2009

Adding a CSS body class to Magento layouts seems very easy.

In case you want different CSS body class for different module pages on frontend, what you need to do is to find the syntax: $this->loadLayout(); in module controller files and add some new lines just below it.

Example:
I want to set the ‘my-profile’ class to all the customer pages on frontend. So I opened the AccountController.php & edited the function indexAction(). Just below the ‘$this->loadLayout();’ I added few lines. So it will look as below.

[php]
$this->loadLayout();

if ($root = $this->getLayout()->getBlock(’root’)) {
$root->addBodyClass(’my-profile’);
}
[/php]

However above code needed to be altered in the core code. So better to follow the below.

To update it via layout XML,


 my-profile

This will affect in the <body> tag.

<body <?php echo $this->getBodyClass()?'class="'.$this->getBodyClass().'"':'' ?>>

Comment Form

About this blog

Hello, Welcome to the Magento Coder.
I am Jignesh Patel, a Web Developer.

About Jignesh

My status