Naming a view helper (partial loop)
Posted: Mon Dec 06, 2010 10:03 pm
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:
You'd do something like:
Using a partial like this:
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:
You would output the above array like this:
With a partial like this:
You can also replace "element" with another name, for instance "item", like this:
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?
Code: Select all
$data = array(
array('item1' => 'bar', 'item2' => 'foo'),
array('item1' => 'something else', 'item2' => 'neato'),
array('item1' => 'bees', 'item2' => 'frogs'),
);Code: Select all
echo $this->partialLoop('items.phtml', $data);Code: Select all
<h1><?php echo $this->escape($this->item1); ?></h1>
<p><?php echo $this->escape($this->item2) ?></p>Code: Select all
$data = array('boo','bar','baz','bog','big','ball');Code: Select all
echo $this->partialElementLoop('items.phtml', $data);Code: Select all
<h1><?php echo $this->escape($this->element); ?></h1>Code: Select all
echo $this->partialElementLoop('items.phtml', $data, 'item')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?