我发现我可以用这个脚本添加自己的布局句柄:
$this->getLayout()->getUpdate()->addHandle('myhandle');然后我检查了Alan布局查看器:http://alanstorm.com/2005/projects/MagentoLayoutViewer.tar.gz
?showLayout=handles
此请求的
句柄
这里有我的句柄,但是我的定制布局xml没有使用。
以下是我的xml:
<?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文件:
<?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?
更新:下面是一些可能接近解决方案的内容:
如果在将新句柄添加到页面后添加此代码:
$this->getLayout()->getUpdate()->fetchPackageLayoutUpdates('myhandle');
$this->getLayout()->generateXml();在此之后,"index.php?showLayout=page“调用在xml中写入句柄代码,但页面不使用它。
发布于 2010-08-17 19:16:08
长和短的这是你真的不想注入布局句柄这种方式。它变得相当复杂(足够复杂,我无法一直跟踪下去,足以应付堆栈溢出的答案),但是
下面是一个实验,尝试修改基本动作控制器中的loadLayout方法
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块预渲染。
//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>')
);发布于 2012-07-03 20:52:02
如果您想要添加一个全新的句柄,最好的解决方案可能是添加带观察者的句柄。如果您有一个有自己的URL的模块,它已经有了它唯一的句柄。
创建一个调用正确句柄的模块。
创建etc/config.xml
<?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的代码:
<?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:
<config>
<modules>
<Ho_AttributeSetHandle>
<active>true</active>
<codePool>local</codePool>
</Ho_AttributeSetHandle>
</modules>
</config> https://stackoverflow.com/questions/3463030
复制相似问题