通过 XML 限制 Magento 产品集合
我有一个我编写的畅销书模块,它运行得很好,但是我希望能够通过 XML 而不是 php/phtml 更改它返回的集合大小。
像这样的东西:
<block type="catalog/product_list" name="bestsellers" limit="3"
template="custom/bestsellers.phtml" />
或这样的东西:
<block type="catalog/product_list" name="bestsellers"
template="custom/bestsellers.phtml">
<action method="setLimit">3</action>
</block>
这可能吗?
我目前正在通过 phtml 更改限制:
->setPageSize(3)
->setCurPage(1);
但这是硬编码且令人讨厌的,我需要能够使用我的 phtml 文件作为模板,用于从任何地方使用 XML + 限制调用畅销书模块的情况XML。
如果有人能阐明这一点,请提前致谢!
I've got a bestsellers module which I've written and it works great, however I want to be able to change the collection size it returns via the XML, rather than the php/phtml.
Something like this:
<block type="catalog/product_list" name="bestsellers" limit="3"
template="custom/bestsellers.phtml" />
or something like:
<block type="catalog/product_list" name="bestsellers"
template="custom/bestsellers.phtml">
<action method="setLimit">3</action>
</block>
Is this possible?
I'm currently changing the limit via the phtml with:
->setPageSize(3)
->setCurPage(1);
But that is hard coded and nasty, I need to be able to use my phtml file as template for many cases of the bestsellers module being called from anywhere with the XML + limit in the XML.
Thanks in advance if anyone can shed light on this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
块
Mage_Catalog_Block_Product_List
继承自Varien_Object
类,该类还包含方法getData()
和setData()
就像魔术方法get*()
和set*()
一样。这些方法允许我们在对象中存储(您猜对了)键控数据。XML 中的
标签允许我们对块实例执行方法调用。第二个示例即将完成,但语法是:相当于:
大致相当于:
通过对象中的数据集,我们现在可以通过
getData()
或通过调用$this->getLimit()
或$this->getData('limit')
的get*()
方法使我们的块代码:您可能应该检查是否存在首先
限制
数据,如果 XML 中未提供任何值,则提供默认值。注意:
标记中的子项名称并不重要。重要的是参数的顺序。我们也可以调用3
并且它仍然有效。The block
Mage_Catalog_Block_Product_List
inherits from theVarien_Object
class which contains the methodsgetData()
andsetData()
, as well as the magic methodsget*()
andset*()
. These methods allow us to store (you guessed it) keyed-data within an object.The
<action />
tags in the XML allows us to perform method calls on the block instances. You're nearly there with your second example, but the syntax is:Which is equivalent to:
Which is roughly equivalent to:
With the data set in the object we can now access through the
getData()
orget*()
methods by calling$this->getLimit()
or$this->getData('limit')
making our block code:You should probably perform a check for the existence of the
limit
data first and provide a default value if none is provided in the XML.Note: The name of the children in the
<action />
tag don't matter. It's the order of the arguments that's important. We could just as well have called<action method="setLimit"><foo>3</foo></action>
and it still would have worked.