如何在magento中重写head.phtml [英] how to rewrite head.phtml in magento

查看:285
本文介绍了如何在magento中重写head.phtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改magento中的页面标题. 在我的app/design/frontend/default/customPackage/template/page/html/head.phtml中,有几行控制着所有页面的标题(我只想修改目录页面的标题)

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>

但是我不想直接从app/design/frontend/default/customPackage/template/page/html中的head.phtml进行修改,相反,我想在我自己的模块模板文件中用另一个head.phtml替换此head.phtml. >代替

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

推荐答案

要回答您的问题,基本上我们需要找到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.

这篇关于如何在magento中重写head.phtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆