通过 XML 限制 Magento 产品集合

发布于 2024-11-27 22:02:54 字数 655 浏览 0 评论 0原文

我有一个我编写的畅销书模块,它运行得很好,但是我希望能够通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2024-12-04 22:02:54

Mage_Catalog_Block_Product_List 继承自 Varien_Object 类,该类还包含方法 getData()setData()就像魔术方法 get*()set*() 一样。这些方法允许我们在对象中存储(您猜对了)键控数据。

XML 中的 标签允许我们对块实例执行方法调用。第二个示例即将完成,但语法是:

<block type="catalog/product_list" name="bestsellers">
    <action method="setLimit"><value>3</value></action>
</block>

相当于:

<block type="catalog/product_list" name="bestsellers">
    <action method="setData"><key>limit</key><value>3</value></action>
</block>

大致相当于:

$block = new Mage_Catalog_Block_Product_List();
$block->setLimit(3);

通过对象中的数据集,我们现在可以通过 getData() 或通过调用 $this->getLimit()$this->getData('limit')get*() 方法使我们的块代码:

->setPageSize($this->getLimit())
->setCurPage(1);

您可能应该检查是否存在首先限制数据,如果 XML 中未提供任何值,则提供默认值。

注意 标记中的子项名称并不重要。重要的是参数的顺序。我们也可以调用 3 并且它仍然有效。

The block Mage_Catalog_Block_Product_List inherits from the Varien_Object class which contains the methods getData() and setData(), as well as the magic methods get*() and set*(). 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:

<block type="catalog/product_list" name="bestsellers">
    <action method="setLimit"><value>3</value></action>
</block>

Which is equivalent to:

<block type="catalog/product_list" name="bestsellers">
    <action method="setData"><key>limit</key><value>3</value></action>
</block>

Which is roughly equivalent to:

$block = new Mage_Catalog_Block_Product_List();
$block->setLimit(3);

With the data set in the object we can now access through the getData() or get*() methods by calling $this->getLimit() or $this->getData('limit') making our block code:

->setPageSize($this->getLimit())
->setCurPage(1);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文