2011-03-14 6 views
2

メソッドのスコープ外にあるメソッドにアクセスする方法を理解するのに問題があります。私の場合はMagento ::: header.htmlでgetBodyClass()を使用する

::::

2columns-left.phtml

からのコードです

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

私はそうのように、header.htmlにgetBodyClassメソッドを使用します::: :

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

しかしgetBodyClassは()Mage_Page_Block_Htmlの方法であるため、それはMage_Page_Block_Html_Headerで$これでは動作しません

誰もがheader.htmlで使用するために、このコード

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

を調整して私を助けることができますか?または私は彼の正しい方向を指す? body要素上のクラスを利用するスタイルシートで

答えて

1

あなたはこのkerfuffleを避けることができCSSルール

を使用して、私たちの宣言、例えば:

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

ページを作成します。/htmlブロック

上記のCSSルールを使用することをお勧めします。しかし、あなた本当に場合は、あなたはブロックのインスタンスを作成することができますし、直接それにアクセス、そのメソッドのpage/htmlブロックにアクセスする必要があります。

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

'ページ/ html'ブロックがすでに作成されている、あなただけの' getBlock(「ルート」) ' – clockworkgeek

+0

@clockする必要があり、答えとして私はそれをupvoteことができるようにすることをポスト:) –

4

たときに、そのブロック体クラスはpage/htmlブロックに設定されていますインスタンス化されます。

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(); 
} 

あなたが別のブロックからそれをつかむことができる唯一の方法は、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のブロックをインスタンス化することです。

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

はアランに感謝、 '$ this-> getLayout() - > getBlock(" page/html ") - > getBodyClass();'は、すべてのクラスを渡すのではなく、 '$ this-> getLayout() - > getBlock( 'root ') - > getBodyClass(); 'でした。非常に便利。 –

関連する問題