how to rewrite head.phtml in magento

2019-07-29 02:58发布

问题:

I want to change the page title in magento. In my app/design/frontend/default/customPackage/template/page/html/head.phtml, there are lines control all page title(what I want just to modify the catalog page's title)

<title>
<?php 
     if ($current_product = Mage::registry('current_product')) { 
        echo substr($current_product->getName() . " - " . Mage::helper('core')->currency($current_product->getFinalPrice(), true, false),0,69);
    } else {

        echo substr(str_replace("- Products","",$this->getTitle()),0,100);
    }
     ?></title>

but I don't want to modify directly from head.phtml in app/design/frontend/default/customPackage/template/page/html, instead, I want to replace this head.phtml with another head.phtml in my own module tempate files.Let's say , make it app/design/frontend/default/customPackage/template/catalog/html/head.phtml instead

回答1:

To answer your question, basically we need to find where is page/html/head.phtml file is defined. Answer is in layout files, more specifically in page.xml. Location is :app/design/frontend/<your_package>/<your_theme>/layout/page.xml. Inside that file, under the handle <default>, you can see

<default translate="label" module="page">
    <label>All Pages</label>
    <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">

        <block type="page/html_head" name="head" as="head">
            <action method="addJs"><script>prototype/prototype.js</script></action>
            <action method="addJs"><script>lib/ccard.js</script></action>
            <action method="addJs"><script>prototype/validation.js</script></action>
            <action method="addJs"><script>scriptaculous/builder.js</script></action>
            -------
        </block>
         ---------
    </block>
</default>

Where

  • <default> is known as layout handler. Blocks that comes under this handler will show in every page in magento.

  • block page/html is your root block. It is the parent block of all other blocks. There should be only one root block per page. You can reference this block using its name root in your custom layout files, in order to alter anything inside this block.

  • block page/html_head is the block which is referenced in your question. This block is use to hold the <head /> section of your page (in terms of html tree). You can see that magento loads some of its core javascripts and css inside this block.

But page/html_head is not set with any template as you already see. Then how page/html/head.phtml came to the seen ??? It should set somewhere in magento. So let us go the backend side of this block, where all of its block methods are defined. The file location is app/code/core/Mage/Page/Block/Html/Head.php. Yes we found out it.

class Mage_Page_Block_Html_Head extends Mage_Core_Block_Template
{
/**
 * Initialize template
 *
 */
protected function _construct()
{
    $this->setTemplate('page/html/head.phtml');
}
 ------
}

So Magento set template for page/html_head block here, through _construct() method. Change it to your template location

 protected function _construct()
 {
    $this->setTemplate('app/design/frontend/default/customPackage/template/catalog/html/head.phtml');
 }

It will now set location of page/html_head block to your custom template file.

If you want to the block file is also untouched, you can rewrite this block file using your own module. In your config.xml file, you should use this

<config>
    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_head>Namespace_Modulename_Block_Html_Head</html_head>
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

and you should define a block file in app/code/local/Namespace/Moduleame/Block/Html/Head.php

<?php
class Namespace_Modulename_Block_Html_Head extends Mage_Page_Block_Html_Head 
{
     protected function _construct()
     {
        $this->setTemplate('app/design/frontend/default/customPackage/template/catalog/html/head.phtml');
     }
}

This way core files are untouched and still you can alter the template path.