views:

296

answers:

2

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?

+1  A: 

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.

Matthijs van den Bos
Interesting. For some reason I thought I wouldn't be able to access the magic database methods of a model class doing it like so. Thought I tested it, but I'll have to retry. Thanks.
Typeoneerror
+2  A: 

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.

Victor Farazdagi
Yeah, coming back to this I ended up using setObjectKey and it worked out perfectly.
Typeoneerror