0
我想在Magento中以编程方式设置块的位置。以编程方式在Magento中设置块的位置
例如我希望我可以设置一个块作为位置:
以下产品视图页
的“内容”在侧栏(左/右)
任何其他块之前/之后。
请提出建议的方式。
我想在Magento中以编程方式设置块的位置。以编程方式在Magento中设置块的位置
例如我希望我可以设置一个块作为位置:
以下产品视图页
的“内容”在侧栏(左/右)
任何其他块之前/之后。
请提出建议的方式。
我找到了解决方案。您可以使用观察器动态设置块位置。 首先在<Namespace>/<Module>/Model
目录下创建Observer.php
。写下面的代码在这个文件中:
class <Namespace>_<Module>_Model_Observer
{
public function set_block($observer)
{
$action = $observer->getEvent()->getAction();
$fullActionName = $action->getFullActionName();
$position = 'right';
$sub_position = 'before="cart_sidebar"';
$myXml = '<reference name="'.$position.'">';
$myXml .= '<block type="obc/obc" name="obc" template="obc/obc.phtml" '.$sub_position.' />';
$myXml .= '</reference>';
$layout = $observer->getEvent()->getLayout();
if ($fullActionName=='catalog_product_view') { //set any action name here
$layout->getUpdate()->addUpdate($myXml);
$layout->generateXml();
}
}
}
现在3210写入以下行来调用观察者:
<events>
<controller_action_layout_generate_blocks_before>
<observers>
<module_block_observer>
<type>singleton</type>
<class><Namespace>_<Module>_Model_Observer</class>
<method>set_block</method>
</module_block_observer>
</observers>
</controller_action_layout_generate_blocks_before>
</events>
现在,您可以随时随地在页面上使用观察者设置你的块的位置。
为什么不能使用insert(blockname,sibling,after,alias)方法? – kaushik 2013-12-11 09:08:17
你有什么试过? ESL可能是一个因素,但这些看起来像面试问题。 – benmarks 2013-03-26 13:03:02