Page 1 of 1

Naming a view helper (partial loop)

Posted: Mon Dec 06, 2010 10:03 pm
by Luke
The existing partialLoop view helper in Zend Framework requires that you provide it with an array of arrays in order for it to be of any use. For example, to display an array like the following:

Code: Select all

$data = array(
    array('item1' => 'bar', 'item2' => 'foo'),
    array('item1' => 'something else', 'item2' => 'neato'),
    array('item1' => 'bees', 'item2' => 'frogs'),
);
You'd do something like:

Code: Select all

echo $this->partialLoop('items.phtml', $data);
Using a partial like this:

Code: Select all

<h1><?php echo $this->escape($this->item1); ?></h1>
<p><?php echo $this->escape($this->item2) ?></p>
That works great when you have an array of arrays but what if you have just a single-dimensional array to loop over and you just want access to each element in the array using some pre-specified name such as "element"? As far as I know, there is no way to do this with the provided partialLoop helper. So I have created a view helper I have temporarily named PartialElementLoop, but I hate the name. It works like this:

Code: Select all

$data = array('boo','bar','baz','bog','big','ball');
You would output the above array like this:

Code: Select all

echo $this->partialElementLoop('items.phtml', $data);
With a partial like this:

Code: Select all

<h1><?php echo $this->escape($this->element); ?></h1>
You can also replace "element" with another name, for instance "item", like this:

Code: Select all

echo $this->partialElementLoop('items.phtml', $data, 'item')
You can also access the array's key in the partial using $this->key.

OK, so yeah, that was quite a lot of explanation and code samples for this simple question. What should I call this view helper? I just can't stand the name "PartialElementLoop". Can you guys think of anything better?

Re: Naming a view helper (partial loop)

Posted: Tue Dec 07, 2010 12:48 am
by Luke
Maybe...

namedPartialLoop?
partialLoopItem?
partialLoopSimple?
partialLoopSingle?

Ugh... none of those sound right. This is really hard :(

Re: Naming a view helper (partial loop)

Posted: Tue Dec 07, 2010 2:18 am
by matthijs
Naming is the most difficult thing in programming. Almost :)

Maybe
partialArrayLoop
partialLoopArray
partialLoopSimplearray

Re: Naming a view helper (partial loop)

Posted: Tue Dec 07, 2010 7:18 am
by Jenk
I'd go with renderElements or something. Partial Loop is just confusing.