Magento ::: Using getBodyClass() in header.html

I am having trouble with working out how to access a method from outside its scope.

In my case ::::

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

That is code from 2columns-left.phtml

I want to use the getBodyClass method in header.html, like so ::::

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

But because getBodyClass() is a method of Mage_Page_Block_Html, it doesn’t work with $this in Mage_Page_Block_Html_Header.

Can anyone help me with adjusting this code

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

for use in header.html? Or point me int he right direction?

2 thoughts on “Magento ::: Using getBodyClass() in header.html”

  1. Use a CSS rule

    You could avoid this kerfuffle and us declarations in your stylesheet that make use of the class that’s on the body element, e.g:

    body.2column-left .header {
       ...
    }
    

    Create the page/html block

    I’d suggest using the CSS rule above. But if you really need access to the page/html block for that method, then you could create an instance of the block and access it directly with:

     $body_classes = $this->getLayout()->createBlock("page/html")->getBodyClass();
    

  2. The body class is set on a page/html block when that block is instantiated.

    public function __construct()
    {
        parent::__construct();
        $this->_urls = array(
            'base'      => Mage::getBaseUrl('web'),
            'baseSecure'=> Mage::getBaseUrl('web', true),
            'current'   => $this->getRequest()->getRequestUri()
        );
    
        $action = Mage::app()->getFrontController()->getAction();
        if ($action) {
            $this->addBodyClass($action->getFullActionName('-'));
        }
    
        $this->_beforeCacheUrl();
    }
    

    The only way you can grab it from another block is to instantiate another page/html.

    <?php
    //from any block template context
    $body_class = $this->getLayout()->createBlock('page/html')->getBodyClass();
    ?>
    ...
    <div class="header <?php echo $body_class?>">
    

    Or to get a reference to an existing page/html block.

    <?php
    $body_class = $this->getLayout()->getBlock('root')->getBodyClass();
    ?>
    

Leave a Reply

Your email address will not be published. Required fields are marked *