且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在magento中重写head.phtml

更新时间:2023-11-30 09:39:28

要回答您的问题,基本上我们需要找到page/html/head.phtml文件的定义位置.答案在布局文件中,更具体地说是在page.xml中.位置是:app/design/frontend/<your_package>/<your_theme>/layout/page.xml.在该文件内的<default>手柄下,您可以看到

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>

哪里

  • <default>被称为布局处理程序.在此之下的块 处理程序将显示在magento的每个页面中.

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

page/html是您的根块.它是所有其他块的父块.每页只能有一个根块.您可以在自定义布局文件中使用其名称root引用此块,以更改此块内的任何内容.

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.

page/html_head是您的问题中引用的块.此块用于保存页面的<head />部分(以html树表示).您会看到magento在此块中加载了其一些核心javascript和css.

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.

但是您已经看到,page/html_head没有设置任何模板.然后page/html/head.phtml是如何出现的?它应该设置在magento中的某个位置.因此,让我们进入该块的后端,在该块的所有块方法都已定义.文件位置是app/code/core/Mage/Page/Block/Html/Head.php.是的,我们找到了.

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

因此,Magento通过_construct()方法在此处为page/html_head块设置了模板.将其更改为您的模板位置

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

现在它将page/html_head块的位置设置为您的自定义模板文件.

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

如果要阻止文件也保持不变,则可以使用自己的模块重写此阻止文件.在您的config.xml文件中,您应该使用

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>

,您应该在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.