ZF:在partialLoop中仅对特定部分使用setObjectKey
我已经弄清楚如何使用 PartialLoop 帮助器的 setObjectKey 方法将模型放入partialLoops中。我想知道是否有一种方法可以指定特定的部分循环使用模型密钥而其他循环则不使用。现在我想我必须做这样的事情:
// sets the object key for ALL partialLoops
$this->partialLoop()->setObjectKey("model");
// do the thing
echo $this->partialLoop("elements/recent-blog.phtml", $this->blogs);
// reset the object key so further partialLoops do NOT use the key
$this->partialLoop()->setObjectKey(null);
有什么办法解决这个问题吗?
I've figured out how to get models into partialLoops using the setObjectKey method of the PartialLoop helper. What I'm wondering if there is a way to specify that specific partial loops use the model key and other ones don't. Right now I think I have to do something like this:
// sets the object key for ALL partialLoops
$this->partialLoop()->setObjectKey("model");
// do the thing
echo $this->partialLoop("elements/recent-blog.phtml", $this->blogs);
// reset the object key so further partialLoops do NOT use the key
$this->partialLoop()->setObjectKey(null);
Any way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议不使用 setObjectKey() 是完全错误的 - 提供的代码肯定无法处理对象数组。直接来自手册:
因此,如果您不希望对象被序列化,则必须使用 setObjectKey()。由于 parial 和partialLoop 帮助程序都可以从控制器和视图访问(与任何其他视图帮助程序一样),因此我倾向于全局启用对象键,并在特定循环中切换它(在循环结束时重新启用)。当然需要额外打字,但似乎效果很好。
Suggestion to NOT using setObjectKey() is plain wrong - provided code would certainly fail to work with array of objects. Straight from the manual:
So, if you don't want your objects to be serialized, you have to use setObjectKey(). Since parial and partialLoop helpers are both accessible from controller and view (as any other view helper), I tend to enable object key globally, and switch it of in particular loop (re-enabling at the loop end). Extra typing for sure, but seems to work well.
我的解决方案是不使用 setObjectKey()。为什么不直接这样做:
它与使用 setObjectKey() 具有相同的效果。在我的博客上的这篇文章中,我对此进行更多解释。
My solution would be NOT using setObjectKey(). Why not just do this:
It has the same effect as using setObjectKey(). In this article on my blog I explain more about this.