Skip to content

Commit

Permalink
Include tag in options.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Apr 12, 2014
1 parent 0f70541 commit 2fc3c43
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/View/Helper/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@ public function media($path, array $options = array()) {
/**
* Build a nested list (UL/OL) out of an associative array.
*
* Options for $options:
*
* - `tag` - Type of list tag to use (ol/ul)
*
* Options for $itemOptions:
*
* - `even` - Class to use for even rows.
Expand All @@ -1084,14 +1088,14 @@ public function media($path, array $options = array()) {
* @param array $list Set of elements to list
* @param string|array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
* @param array $itemOptions Additional HTML attributes of the list item (LI) tag
* @param string $tag Type of list tag to use (ol/ul)
* @return string The nested list
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::nestedList
*/
public function nestedList(array $list, array $options = [], array $itemOptions = [], $tag = 'ul') {
$items = $this->_nestedListItem($list, $options, $itemOptions, $tag);
public function nestedList(array $list, array $options = [], array $itemOptions = []) {
$options += array('tag' => 'ul');
$items = $this->_nestedListItem($list, $options, $itemOptions);
return $this->formatTemplate($tag, [
'attrs' => $this->templater()->formatAttributes($options),
'attrs' => $this->templater()->formatAttributes($options, ['tag']),
'content' => $items
]);
}
Expand All @@ -1100,19 +1104,18 @@ public function nestedList(array $list, array $options = [], array $itemOptions
* Internal function to build a nested list (UL/OL) out of an associative array.
*
* @param array $items Set of elements to list
* @param array $options Additional HTML attributes of the list (ol/ul) tag
* @param array $options Options and additional HTML attributes of the list (ol/ul) tag
* @param array $itemOptions Additional HTML attributes of the list item (LI) tag
* @param string $tag Type of list tag to use (ol/ul)
* @return string The nested list element
* @see HtmlHelper::nestedList()
*/
protected function _nestedListItem($items, $options, $itemOptions, $tag) {
protected function _nestedListItem($items, $options, $itemOptions) {
$out = '';

$index = 1;
foreach ($items as $key => $item) {
if (is_array($item)) {
$item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
$item = $key . $this->nestedList($item, $options, $itemOptions);
}
if (isset($itemOptions['even']) && $index % 2 === 0) {
$itemOptions['class'] = $itemOptions['even'];
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,9 @@ public function testNestedList() {
);
$this->assertTags($result, $expected);

$result = $this->Html->nestedList($list, array('tag' => 'ol'));
$this->assertTags($result, $expected);

$result = $this->Html->nestedList($list, array('class' => 'list'));
$expected = array(
array('ul' => array('class' => 'list')),
Expand Down

0 comments on commit 2fc3c43

Please sign in to comment.