Magento 1.4 Magento:::在header.html中使用getBodyClass()

Magento 1.4 Magento:::在header.html中使用getBodyClass(),magento-1.4,magento,Magento 1.4,Magento,我在解决如何从其范围外访问方法时遇到问题 就我而言: <body<?php echo $this->getBodyClass()?' class="'.Mage::app()->getStore()->getCode().' '.$this->getBodyClass().'"':'' ?>> > 这是来自2列s-left.phtml的代码 我想在header.html中使用getBodyClass方法,如下所示: <div class=

我在解决如何从其范围外访问方法时遇到问题

就我而言:

<body<?php echo $this->getBodyClass()?' class="'.Mage::app()->getStore()->getCode().' '.$this->getBodyClass().'"':'' ?>>
>
这是来自2列s-left.phtml的代码

我想在header.html中使用getBodyClass方法,如下所示:

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

使用CSS规则

您可以避免样式表中使用body元素上的类的这种混乱和us声明,例如:

body.2column-left .header {
   ...
}
创建页面/html块

我建议使用上面的CSS规则。但是,如果您确实需要访问该方法的
页面/html
块,则可以创建该块的实例,并直接使用以下方法访问它:

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

实例化块时,在
页面/html
块上设置body类

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();
}
从另一个块获取它的唯一方法是实例化另一个
页面/html

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

已经创建了
page/html
块,您只需要
getBlock('root')
感谢Alan,使用
$this->getLayout()->createBlock(“page/html”)->getBodyClass()
没有给我所有的类,而
$this->getLayout()->getBlock('root')->getBodyClass()没有。非常有用。
<?php
//from any block template context
$body_class = $this->getLayout()->createBlock('page/html')->getBodyClass();
?>
...
<div class="header <?php echo $body_class?>">
<?php $body_class = $this->getLayout()->getBlock('root')->getBodyClass(); ?>