2013-05-15 36 views
2

我有一个自定义类别属性,我想添加到正文类。至于我能找到什么人做的是Magento:在正文类中显示自定义属性

  1. 覆盖的CategoryController并添加类似$root->addBodyClass($category->getMyAttribute());但我不希望覆盖核心类...

  2. 在他们增加了管理面板像<reference name=”root”><action method=”addBodyClass”><className>caravan-motorhome-lighting</className></action></reference>这样的每个类别都不使用属性本身,而是直接添加类。因为我已经有了一个属性,我当然不想克隆它并以这种方式添加类。

那么我最喜欢的解决办法是一些布局更新,我可以添加到该说

<reference name=”root”> 
    <action method=”addBodyClass”> 
     <className> 
      get value of my custom attribute here dynamically 
     </className> 
    </action> 
</reference> 

没有人有一个想法local.xml中如何这可能是工作或其他想法,我没有连想都没想?

回答

5

您可以使用Magento布局XML的一个非常酷的功能来实现这一点。你需要一个模块来实现它。要么专门为此创建一个模块,要么使用主题模块(如果有的话) - 这取决于您决定什么是最好的。

我会告诉你一个例子,我将添加含有类body标签的ID类:

在我的布局XML,我会通过catalog_category_default手柄,以添加。这样,我可以稍后使用Mage::registry('current_category')来检索当前类别。因此,在您的布局XML中执行类似如下的操作:

<catalog_category_default> 
    <reference name="root"> 
     <action method="addBodyClass"> 
      <className helper="mymodule/my_helper/getCategoryClass" /> 
     </action> 
    </reference> 
</catalog_category_default> 

此属性是重要部分:helper="mymodule/my_helper/getCategoryClass"。这相当于在代码中调用Mage::helper('mymodule/my_helper')->getCategoryClass();

无论从该函数返回什么,都将用作<className>节点的值。您可能想要使用您认为更合适的不同帮手,这取决于您自己决定。

进行着与例如,下面的功能:

public function getCategoryClass() { 
    return 'category-id-' . Mage::registry('current_category')->getId(); 
} 

你会想,这样它检索您的属性的值更改代码。例如由Mage::registry('current_category')返回的类别上的getMyAttribute()

另外,您需要确保返回的东西适合作为CSS类。在这个例子中,我们不需要做任何事情,因为ID将始终只是数字,将被附加到category-id-。如果你的属性的值并不总是为了安全起见,你可能要考虑使用的东西like this

It works!

+0

这听起来很惊人的 - 正是我一直在寻找。我会尝试一下! –

+0

作品像一个魅力 –

+0

@RiaElliger优秀的,很高兴听到它。 –

相关问题