Magento - 无法通过自定义模块自定义标题块

时间:2014-10-02 05:53:15

标签: magento

首先,我可以通过

轻松更改标题模板
<reference name="header">
      <action method="setTemplate"><template>customizer/header.phtml</template></action>
</reference>

但是,我想在头块中添加一些函数,所以我尝试扩展:

class My_Customizer_Block_Header extends Mage_Page_Block_Html_Header

所以我将自定义布局xml更改为

<reference name="header">
      <block type="customizer/header" name="header" as="header" template="customizer/header.phtml" />
</reference>

现在,$this->getTemplate()返回null,($this->hasData('template')也返回null

即使我将xml更改为

<reference name="header">
      <block type="customizer/header" name="header" as="header">
             <action method="setTemplate"><template>customizer/header.phtml</template></action>
      </block>
</reference>

它仍然不起作用。

我也尝试扩展Mage_Core_Block_Template,但它不起作用。

我不知道为什么我无法通过自定义模块为标头设置自定义模板。 有人问here,但答案对我来说不够明确。

注意

  • 我不想将Mage/Page/Block/Html/Header.php复制到local目录。

  • 我不想通过config.xml完全重写头类,只需更改自定义句柄中的标题

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以为此目的创建模块

<?xml version="1.0"?>
<config>
    <modules>
        <My_Customheader>
          <version>0.1.0</version>
        </My_Customheader>
    </modules>
    <global>
       <helpers>
         <customheader>
            <class>My_Customheader_Helper</class>
         </customheader>
    </helpers>
    <blocks>
        <customheader>
           <class>My_Customheader_Block</class>
        </customheader>
    </blocks>
  </global>   
 </config>

扩展My_Customheader_Block_Html_Header扩展Mage_Core_Block_Template

在此路径中创建目录和文件app \ code \ local \ My \ Customheader \ Block \ Html \ Header.php

<?php class My_Customheader_Block_Html_Header extends Mage_Core_Block_Template
{

 public function _construct()
{
  //change template path
    $this->setTemplate('customheader/html/header.phtml');
}

public function mycustomfunction(){
   //Write you logic here
 }

/**
 * Check if current url is url for home page
 *
 * @return true
 */
public function getIsHomePage()
{
    return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
}

public function setLogo($logo_src, $logo_alt)
{
    $this->setLogoSrc($logo_src);
    $this->setLogoAlt($logo_alt);
    return $this;
}

public function getLogoSrc()
{
    if (empty($this->_data['logo_src'])) {
        $this->_data['logo_src'] = Mage::getStoreConfig('design/header/logo_src');
    }
    return $this->getSkinUrl($this->_data['logo_src']);
}

public function getLogoSrcSmall()
{
    if (empty($this->_data['logo_src_small'])) {
        $this->_data['logo_src_small'] = Mage::getStoreConfig('design/header/logo_src_small');
    }
    return $this->getSkinUrl($this->_data['logo_src_small']);
}

public function getLogoAlt()
{
    if (empty($this->_data['logo_alt'])) {
        $this->_data['logo_alt'] = Mage::getStoreConfig('design/header/logo_alt');
    }
    return $this->_data['logo_alt'];
}

/**
 * Retrieve page welcome message
 *
 * @deprecated after 1.7.0.2
 * @see Mage_Page_Block_Html_Welcome
 * @return mixed
 */
public function getWelcome()
{
    if (empty($this->_data['welcome'])) {
        if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->_data['welcome'] = $this->__('Welcome, %s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getName()));
        } else {
            $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
        }
    }

    return $this->_data['welcome'];
}
}

更改第91行的调用块文件的page.xml文件 来自

<block type="page/html_header" name="header" as="header">

<block type="customheader/html_header" name="header" as="header">

创建目录和文件app \ design \ frontend \ yourtheme \ default \ template \ customheader \ html \ header.phtml

在header.phtml文件中编写代码

谢谢希望这对你的

有所帮助