Naming a view helper (partial loop)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Naming a view helper (partial loop)

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Naming a view helper (partial loop)

Post by Luke »

Maybe...

namedPartialLoop?
partialLoopItem?
partialLoopSimple?
partialLoopSingle?

Ugh... none of those sound right. This is really hard :(
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Naming a view helper (partial loop)

Post by matthijs »

Naming is the most difficult thing in programming. Almost :)

Maybe
partialArrayLoop
partialLoopArray
partialLoopSimplearray
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Naming a view helper (partial loop)

Post by Jenk »

I'd go with renderElements or something. Partial Loop is just confusing.
Post Reply