Question

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?

Was it helpful?

Solution

Suggestion to NOT using setObjectKey() is plain wrong - provided code would certainly fail to work with array of objects. Straight from the manual:

If your model is an object, you may want to have it passed as an object to the partial script, instead of serializing it to an array of variables. You can do this by setting the 'objectKey' [..skip..]

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.

OTHER TIPS

My solution would be NOT using setObjectKey(). Why not just do this:

echo $this->partialLoop("elements/recent-blog.phtml", array('model' => $this->blogs));

It has the same effect as using setObjectKey(). In this article on my blog I explain more about this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top