Magento xml布局奇怪的行为

Posted

技术标签:

【中文标题】Magento xml布局奇怪的行为【英文标题】:Magento xml layout strange behavior 【发布时间】:2018-04-19 13:57:38 【问题描述】:

我正在安装 1.9.3 Magento,尝试优化现有应用程序。

有一个自定义主题,它定义了自定义布局,我发现一大堆东西:到处都是重复的文件,完全荒谬的核心行为覆盖,所以我试图整理所有东西,一天后我成功了,除非有什么最后一步发生的事情真的很奇怪。

我有 2 个主要布局文件:local.xml and page.xml,我想将它们合并到一个文件中。

一切正常,直到我将所有自定义头部行为(清理上传的资产放在单个部分中),但一旦我合并自定义页面布局,一切都会停止工作。

我的意思是:

page.xml

<layout version="0.1.0">
    <default>
        <label>All Pages</label>
            <block type="page/html_head" name="head" as="head">
                    <action method="addJs"><script>prototype/prototype.js</script></action>
                    <action ..... more js and css.../>
            </block>
            <block...../>
    </default>

    <!-- Custom page layout handles -->
    <page_empty ..../>
    
    <page_one_column ..../>
    
    <page_two_columns_left ..../>
    ....
</layout>

local.xml 中剩下的内容

<layout>
    <cms_index_index translate="label">
        <label>CMS Home Page</label>
        <reference name="head"></reference>
    </cms_index_index>
    
    <cms_page>
        <reference name="content">
            <action method="unsetChild">
                <alias>cms.wrapper</alias>
            </action>
            <block type="cms/page" name="cms_page"/>
        </reference>
    </cms_page>
    
    <catalog_product_view .../>
    
    <catalog_category_layered .../>
    
    <catalog_category_default .../>   
</layout>

当我尝试合并这两种布局时,问题就出现了:迷你购物车和搜索表单消失,主页中的其他元素被弄乱和重复。

可能的原因是什么?自定义主题中的其他布局文件中没有其他影响主页或其他类似元素的重新定义。显然还有其他布局定义,但每个都有自己的角色。 我真的不知道在哪里可以搜索。 现在我可以将这两个文件分开,一切正常。

编辑: 这是我清理的旧 local.xml。

<?xml version="1.0"?>
<layout>
    <default>
        
        <!--CSS and JS Files-->
        <reference name="head">
          <!-- Modifiche per Cas***cheria-->  
          <action method="addItem"><type>skin_js</type><script>js/prodotto/product.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/prodotto/configurable.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/prodotto/calendar.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/prodotto/calendar-setup.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/modernizr.custom.min.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/selectivizr.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/matchMedia.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/matchMedia.addListener.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/enquire.js</script></action>
          <!-- 
          <action method="addItem"><type>skin_js</type><script>js/vendor/app.js</script></action>
          -->
          <action method="addItem"><type>skin_js</type><script>js/vendor/slideshow.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/imagesloaded.js</script></action>
          <action method="removeItem"><type>skin_js</type><name>js/minicart.js</name></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/minicart.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/prodotto/jquery.elevateZoom-3.0.8.min.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/msrp.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/vendor/msrp_rwd.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/jquery.flexslider-min.js</script></action>
          <action method="addItem"><type>skin_js</type><script>js/main.js</script></action>


          <action method="addItem"><type>skin_css</type><name>css/calendar-win2k-1.css</name></action> 

        </reference>
     

        
        <remove name="footer_links"/>
        <remove name="footer_links2"/>
       
    </default>

<cms_index_index translate="label">
    <label>CMS Home Page</label>
             <reference name="head"></reference>

</cms_index_index>
<cms_page>
        <reference name="content">
            <action method="unsetChild">
                <alias>cms.wrapper</alias>
            </action>
            <block type="cms/page" name="cms_page"/>
        </reference>
    </cms_page>
 <!--Product View-->
    <catalog_product_view>
        <!--CSS and JS Files-->
        <reference name="head">             
        </reference>
          <reference name="content">
          <!--  <block type="catalog/product_list_related" name="catalog.product.related" as="related_products" template="catalog/product/list/related2.phtml" />-->
          <remove name="product.info.extrahint"/>
          
          <remove name="product.reviews"/>

           <block type="core/template" name="slider" template="catalog/product/list_session.phtml" />
          </reference>
    </catalog_product_view>
        <catalog_category_layered>
       <reference name="head">
            <!-- Remove items which the RWD package is not dependent upon -->
                <action method="removeItem"><type>skin_js</type><name>js/lib/jquery-1.10.2.min.js</name></action>
        </reference>
      <reference name="root">
        <action method="setTemplate">
          <template>page/2columns-left.phtml</template> 
        </action>
      </reference>
        <reference name="product_list">
          <action method="setColumnCount"><count>3</count></action>          
      </reference>
    </catalog_category_layered>
    <catalog_category_default>
      <reference name="root">
        <action method="setTemplate">
          <template>page/2columns-left.phtml</template> 
        </action>
      </reference>
       <reference name="product_list">
          <action method="setColumnCount"><count>3</count></action>          
      </reference>
    </catalog_category_default>
   
</layout>

编辑 2:这是我实际使用的 page.xml 的最终版本

<layout version="0.1.0">
    <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/jquery/jquery-1.10.2.min.js</script></action> 
                <action method="addJs"><script>lib/jquery/noconflict.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>
                <action method="addJs"><script>scriptaculous/effects.js</script></action>
                <action method="addJs"><script>scriptaculous/dragdrop.js</script></action>
                <action method="addJs"><script>scriptaculous/controls.js</script></action>
                <action method="addJs"><script>scriptaculous/slider.js</script></action>
                <action method="addJs"><script>varien/js.js</script></action>
                <action method="addJs"><script>varien/form.js</script></action>
                <action method="addJs"><script>mage/translate.js</script></action>
                <action method="addJs"><script>mage/cookies.js</script></action>

                <block type="page/js_cookie" name="js_cookies" template="page/js/cookie.phtml"/>

                <!-- Remove items which the RWD package is not dependent upon -->
                <action method="removeItem"><type>skin_js</type><name>js/ie6.js</name></action>

                <!-- Add vendor dependencies -->
                <action method="addItem"><type>skin_js</type><name>js/lib/modernizr.custom.min.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/selectivizr.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/matchMedia.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/matchMedia.addListener.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/enquire.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/app.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/jquery.cycle2.min.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/jquery.cycle2.swipe.min.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/slideshow.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/lib/imagesloaded.js</name></action>
                <action method="addItem"><type>skin_js</type><name>js/minicart.js</name></action>
                <action method="addItem"><type>skin_js</type><script>js/msrp.js</script></action>
                <action method="addItem"><type>skin_js</type><script>js/msrp_rwd.js</script></action>
                
                <action method="addItem"><type>skin_js</type><script>js/jquery.flexslider-min.js</script></action>
                <action method="addItem"><type>skin_js</type><script>js/main.js</script></action>

                <!-- Add stylesheets with no media queries for use in IE 8 and below -->
                <action method="addItem"><type>skin_css</type><name>css/styles-ie8.css</name><params/><if><![CDATA[ (lte IE 8) & (!IEMobile)]]></if></action>
                <action method="addItem"><type>skin_css</type><name>css/madisonisland-ie8.css</name><params/><if><![CDATA[ (lte IE 8) & (!IEMobile)]]></if></action>

                <!-- Add stylesheets with media queries for use by modern browsers -->
                <action method="addItem"><type>skin_css</type><name>css/fonts/roboto-condensed/stylesheet.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>
                <action method="addItem"><type>skin_css</type><name>css/styles.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>
                <action method="addItem"><type>skin_css</type><name>css/fonts/fontello3/css/fontello.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>
               
                <action method="addItem"><type>skin_css</type><name>css/madisonisland.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>
                 <action method="addItem"><type>skin_css</type><name>css/flexslider.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>

                <!-- Sets viewport meta tag using text block -->
                <block type="core/text" name="head.viewport">
                    <action method="setText"><text><![CDATA[<meta name="viewport" content="initial-scale=1.0, width=device-width" />]]>&#10;</text></action>
                </block>
            </block>

            <block type="core/text_list" name="after_body_start" as="after_body_start" translate="label">
                <label>Page Top</label>
            </block>

            <block type="page/html_notices" name="global_notices" as="global_notices" template="page/html/notices.phtml" />

            <block type="page/html_header" name="header" as="header">
                <block type="page/template_links" name="top.links" as="topLinks"/>
                <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <block type="core/text_list" name="top.menu" as="topMenu" translate="label">
                    <label>Navigation Bar</label>
                    <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml">
                        <block type="page/html_topmenu_renderer" name="catalog.topnav.renderer" template="page/html/topmenu/renderer.phtml"/>
                    </block>
                </block>
                <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
                    <label>Page Header</label>
                    <action method="setElementClass"><value>top-container</value></action>
                </block>
                <block type="page/html_welcome" name="welcome" as="welcome"/>
            </block>

            <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>

            <block type="core/text_list" name="left_first" as="left_first" translate="label">
                <label>Left Column First (shows above main column on smaller screens)</label>
            </block>

            <block type="core/text_list" name="left" as="left" translate="label">
                <label>Left Column</label>
            </block>

            <block type="core/messages" name="global_messages" as="global_messages"/>
            <block type="core/messages" name="messages" as="messages"/>

            <block type="core/text_list" name="content" as="content" translate="label">
                <label>Main Content Area</label>
            </block>

            <block type="core/text_list" name="right" as="right" translate="label">
                <label>Right Column</label>
            </block>

            <block type="page/html_wrapper" name="footer.before" as="footer_before" translate="label">
                <label>Page Footer</label>
                <action method="setElementClass"><value>footer-before-container</value></action>
            </block>

            <block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
                <block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
                    <label>Page Footer</label>
                    <action method="setElementClass"><value>bottom-container</value></action>
                </block>
                <block type="page/switch" name="store_switcher" as="store_switcher" after="*" template="page/switch/stores.phtml"/>
                <block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml">
                    <action method="setTitle"><title>Quick Links</title></action>
                </block>
                <block type="page/template_links" name="footer_links2" as="footer_links2" template="page/template/links.phtml">
                    <action method="setTitle"><title>Account</title></action>
                </block>
                <!-- This static block can be created and populated in admin. The footer_links cms block can be used as a starting point. -->
                <!--<block type="cms/block" name="footer_social_links">
                    <action method="setBlockId"><block_id>footer_social_links</block_id></action>
                </block>-->
            </block>

            <block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">
                <label>Page Bottom</label>
                <block type="page/html_cookieNotice" name="global_cookie_notice" as ="global_cookie_notice" template="page/html/cookienotice.phtml" before="-" />
            </block>
        </block>
        
        <remove name="footer_links"/>
        <remove name="footer_links2"/>

        <block type="core/profiler" output="toHtml" name="core_profiler"/>
    </default>


    <catalog_product_view>
        <!--CSS and JS Files-->
        <reference name="head">             
        </reference>
          <reference name="content">
          <remove name="product.info.extrahint"/>
          <remove name="product.reviews"/>
           <block type="core/template" name="slider" template="catalog/product/list_session.phtml" />
          </reference>
    </catalog_product_view>
    
    <catalog_category_layered>
        <reference name="head"></reference>
        <reference name="root">
            <action method="setTemplate">
                <template>page/2columns-left.phtml</template> 
            </action>
        </reference>
        <reference name="product_list">
          <action method="setColumnCount"><count>3</count></action>          
      </reference>
    </catalog_category_layered>
    
    <catalog_category_default>
        <reference name="root">
            <action method="setTemplate">
                <template>page/2columns-left.phtml</template> 
            </action>
        </reference>
        <reference name="product_list">
          <action method="setColumnCount"><count>3</count></action>          
        </reference>
    </catalog_category_default> 
</layout>

【问题讨论】:

【参考方案1】:

您要将它们合并到哪个文件中?我假设您将所有内容都移动到 local.xml 并从您的主题中删除 page.xml 文件?

这个问题是 local.xml 是最后加载的,所以当你从它替换块时,对 page.xml 的任何更新都将丢失。

方法如下: page.xmlcatalog.xmlcheckout.xml 之前加载。目录和结帐将附加块添加到标题(例如迷你购物车和搜索表单)。但是由于local.xml是最后加载的,所以之前添加到页面xml中的任何块,例如catalog.xmlcheckout.xml,都会在这个过程中丢失。

您需要做的是引用您需要更改的块,而不是替换它们。所以如果你需要在头部添加一个css文件,你可以这样做:

<default>
    <reference name="head">
        <!-- add blocks and actions here -->
    </reference>
</default>

您需要区分默认值和自定义修改,并通过引用将这些修改移至local.xml。然后你可以从你的主题中删除page.xml

或者...您可以将代码分开。

【讨论】:

是的,实际上我想要的是删除 page.xml 并只保留 local.xml。我知道回退到 rwd/default 或 base/default 主题,但我检查了它们,基本上,我的自定义 page.xml 是 rwd/default 中 page.xml 的副本,所以我认为不会有问题如果他们被评估。目前我没有在做这方面的工作,但我会回去让你知道的。谢谢你现在的帮助。竖起大拇指! 如果 page.xml 是 rwd/default 的副本,则无需将其合并到 local.xml。只需从您的主题中删除 page.xml。 好吧,我真的尝试了所有可能的组合:删除我的 page.xml 中的所有重复项,将所有内容放入本地并删除页面,但每当我拿出它时,迷你车仍然消失。我最终合并了 page.xml 中的所有内容并删除了无用的句柄,只在其他页面中保留了默认布局声明和自定义块。但这真的让我很不安,我不能完全做到。确实如此 嗯。您可以发布导致问题的合并local.xml 吗? 当然,现在它是我的 page.xml,我完全删除了本地。我会把它添加到我的问题中【参考方案2】:

为什么还要为cms_index_index 句柄定义头部引用?

请提供有关您的清理工作的更多背景信息并显示完整的文件。与原始 local.xml 的差异会很有帮助

【讨论】:

只是为了完整性而保留这些句柄,而不是覆盖所有内容。如果您觉得它有用,我将粘贴整个旧本地,但我已删除所有重复的资产,因此所有引用都不再有效

以上是关于Magento xml布局奇怪的行为的主要内容,如果未能解决你的问题,请参考以下文章

Magento:如何将小部件放入布局 xml?

Magento 我的帐户布局 XML 问题

xml 覆盖magento产品视图布局

xml Magento布局

如何使用 Magento 布局 xml 文件中的操作方法

xml magento 1中的自定义CMS页面布局