首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento在小部件中使用自己的句柄不起作用

问Magento在小部件中使用自己的句柄不起作用
EN

Stack Overflow用户
提问于 2010-08-11 21:34:01
回答 2查看 3.3K关注 0票数 4

我发现我可以用这个脚本添加自己的布局句柄:

代码语言:javascript
复制
$this->getLayout()->getUpdate()->addHandle('myhandle');

然后我检查了Alan布局查看器:http://alanstorm.com/2005/projects/MagentoLayoutViewer.tar.gz

?showLayout=handles

此请求的

句柄

  1. default
  2. cms_page
  3. STORE_default
  4. THEME_frontend_default_default
  5. cms_index_index
  6. page_two_columns_left
  7. customer_logged_out
  8. myhandle

这里有我的句柄,但是我的定制布局xml没有使用。

以下是我的xml:

代码语言:javascript
复制
<?xml version="1.0"?>
<layout version="0.1.0">

    <myhandle>
        <reference name="head">
          <action method="addJs"><script>test/your.js</script></action>
        </reference>
    </myhandle>
</layout>

这很好,因此加载了xml文件:

代码语言:javascript
复制
<?xml version="1.0"?>
<layout version="0.1.0">

    <default>
        <reference name="head">
          <action method="addJs"><script>test/your.js</script></action>
        </reference>
    </default>
</layout>

怎么了?为什么这个解决方案行不通?

如果这不是正确的方法,我如何为小部件使用的页面添加自定义css和javascript?

更新:下面是一些可能接近解决方案的内容:

如果在将新句柄添加到页面后添加此代码:

代码语言:javascript
复制
$this->getLayout()->getUpdate()->fetchPackageLayoutUpdates('myhandle');
$this->getLayout()->generateXml();

在此之后,"index.php?showLayout=page“调用在xml中写入句柄代码,但页面不使用它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-17 19:16:08

长和短的这是你真的不想注入布局句柄这种方式。它变得相当复杂(足够复杂,我无法一直跟踪下去,足以应付堆栈溢出的答案),但是

  1. 如果在从控制器调用$this->loadLayout()之前添加句柄,那太快了;如果在调用$this->loadLayout()之后添加句柄,则
  2. 太晚了。

下面是一个实验,尝试修改基本动作控制器中的loadLayout方法

代码语言:javascript
复制
File: app/code/core/Mage/Core/Controller/Varien/Action.php
public function loadLayout($handles=null, $generateBlocks=true, $generateXml=true)
{    
    // if handles were specified in arguments load them first
    if (false!==$handles && ''!==$handles) {
        $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
    }

    //YOUR NEW CALL HERE
    $this->getLayout()->getUpdate()->addHandle('myhandle');         

    ...

这应该有效,并应用您的布局。现在,在生产中这样做将是一个糟糕的想法,我提到它只是为了向您展示需要在非常特定的时间布局呈现过程中添加句柄。作为一个最终程序员,插入布局句柄并不是你真正的工作。

布局系统是指位于设计器和原始PHP系统代码之间的一个层。由于您显然能够编写PHP代码,所以我只想直接将您的javascript注入head块预渲染。

代码语言:javascript
复制
    //from a controller, but could be modified to be used elsewhere
    //also pseudo code
$this->getLayout()->getBlock('header')->append(
    $this->getLayout()
    ->createBlock('core/text', 'some-unique-name')
    ->setText('<script type="text/javascript" src="/foo/baz/bar.js"></script>')
);
票数 7
EN

Stack Overflow用户

发布于 2012-07-03 20:52:02

如果您想要添加一个全新的句柄,最好的解决方案可能是添加带观察者的句柄。如果您有一个有自己的URL的模块,它已经有了它唯一的句柄。

创建一个调用正确句柄的模块。

创建etc/config.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Ho_AttributeSetHandle>
            <version>0.1.0</version>
        </Ho_AttributeSetHandle>
    </modules>
    <global>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <attributesethandle>
                        <class>Ho_AttributeSetHandle_Model_Observer</class>
                        <method>addAttributeSetHandle</method>
                    </attributesethandle>
                </observers>
            </controller_action_layout_load_before>
        </events>
    </global>
</config>

在我的例子中,Model/Observer.php的代码:

代码语言:javascript
复制
<?php
class Ho_AttributeSetHandle_Model_Observer
{
    /**
     * Converts attribute set name of current product to nice name ([a-z0-9_]+).
     * Adds layout handle PRODUCT_ATTRIBUTE_SET_<attribute_set_nicename> after
     * PRODUCT_TYPE_<product_type_id> handle
     *
     * Event: controller_action_layout_load_before
     *
     * @param Varien_Event_Observer $observer
     */
    public function addAttributeSetHandle(Varien_Event_Observer $observer)
    {
        $product = Mage::registry('current_product');

        /**
         * Return if it is not product page
         */
        if (!($product instanceof Mage_Catalog_Model_Product)) {
            return;
        }

        $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId());

        /* Convert attribute set name to alphanumeric + underscore string */
        $niceName = strpos($product->formatUrlKey($attributeSet->getAttributeSetName()), '-') !== FALSE
                    ? str_replace('-', '_', $product->formatUrlKey($attributeSet->getAttributeSetName()))
                    : $product->formatUrlKey($attributeSet->getAttributeSetName());
        $niceName = strpos($niceName, ' ') !== FALSE ? str_replace(' ', '_', $product->formatUrlKey($niceName)) : $niceName;

        /* @var $update Mage_Core_Model_Layout_Update */
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $handles = $update->getHandles(); // Store all handles in a variable
        $update->resetHandles(); // Remove all handles

        /**
         * Rearrange layout handles to ensure PRODUCT_ATTRIBUTE_SET_<attribute_set>
         * handle is added last
         */
        foreach ($handles as $handle) {
            $update->addHandle($handle);
            if ($handle == 'PRODUCT_TYPE_' . $product->getTypeId()) {
                $update->addHandle('PRODUCT_ATTRIBUTE_SET_' . $niceName);
            }
        }
    }
}

不要忘记创建app/etc/modules/Ho_AttributeSetHandle.xml:

代码语言:javascript
复制
<config>
    <modules>
        <Ho_AttributeSetHandle>
            <active>true</active>
            <codePool>local</codePool>
        </Ho_AttributeSetHandle>
    </modules>
</config>  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3463030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档