Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
d5bd9853
Commit
d5bd9853
authored
Jun 25, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Html::ol() and Html::ul()
parent
3eccd850
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
0 deletions
+121
-0
Html.php
framework/yii/helpers/base/Html.php
+65
-0
HtmlTest.php
tests/unit/framework/helpers/HtmlTest.php
+56
-0
No files found.
framework/yii/helpers/base/Html.php
View file @
d5bd9853
...
...
@@ -9,6 +9,7 @@ namespace yii\helpers\base;
use
Yii
;
use
yii\base\InvalidParamException
;
use
yii\helpers\ArrayHelper
;
use
yii\web\Request
;
use
yii\base\Model
;
...
...
@@ -837,6 +838,70 @@ class Html
}
/**
* Generates an unordered list.
* @param array|\Traversable $items the items for generating the list. Each item generates a single list item.
* Note that items will be automatically HTML encoded if `$options['encode']` is not set or true.
* @param array $options options (name => config) for the radio button list. The following options are supported:
*
* - encode: boolean, whether to HTML-encode the items. Defaults to true.
* - item: callable, a callback that is used to generate each individual list item.
* The signature of this callback must be:
*
* ~~~
* function ($index, $item)
* ~~~
*
* where $index is the array key corresponding to `$item` in `$items`. The callback should return
* the whole list item tag.
*
* @return string the generated unordered list. An empty string is returned if `$items` is empty.
*/
public
static
function
ul
(
$items
,
$options
=
array
())
{
if
(
empty
(
$items
))
{
return
''
;
}
$tag
=
isset
(
$options
[
'tag'
])
?
$options
[
'tag'
]
:
'ul'
;
$encode
=
!
isset
(
$options
[
'encode'
])
||
$options
[
'encode'
];
$formatter
=
isset
(
$options
[
'item'
])
?
$options
[
'item'
]
:
null
;
unset
(
$options
[
'tag'
],
$options
[
'encode'
],
$options
[
'item'
]);
$results
=
array
();
foreach
(
$items
as
$index
=>
$item
)
{
if
(
$formatter
!==
null
)
{
$results
[]
=
call_user_func
(
$formatter
,
$index
,
$item
);
}
else
{
$results
[]
=
'<li>'
.
(
$encode
?
static
::
encode
(
$item
)
:
$item
)
.
'</li>'
;
}
}
return
static
::
tag
(
$tag
,
"
\n
"
.
implode
(
"
\n
"
,
$results
)
.
"
\n
"
,
$options
);
}
/**
* Generates an ordered list.
* @param array|\Traversable $items the items for generating the list. Each item generates a single list item.
* Note that items will be automatically HTML encoded if `$options['encode']` is not set or true.
* @param array $options options (name => config) for the radio button list. The following options are supported:
*
* - encode: boolean, whether to HTML-encode the items. Defaults to true.
* - item: callable, a callback that is used to generate each individual list item.
* The signature of this callback must be:
*
* ~~~
* function ($index, $item)
* ~~~
*
* where $index is the array key corresponding to `$item` in `$items`. The callback should return
* the whole list item tag.
*
* @return string the generated ordered list. An empty string is returned if `$items` is empty.
*/
public
static
function
ol
(
$items
,
$options
=
array
())
{
$options
[
'tag'
]
=
'ol'
;
return
static
::
ul
(
$items
,
$options
);
}
/**
* Generates a label tag for the given model attribute.
* The label text is the label associated with the attribute, obtained via [[Model::getAttributeLabel()]].
* @param Model $model the model object
...
...
tests/unit/framework/helpers/HtmlTest.php
View file @
d5bd9853
...
...
@@ -366,6 +366,62 @@ EOD;
)));
}
public
function
testUl
()
{
$data
=
array
(
1
,
'abc'
,
'<>'
,
);
$expected
=
<<<EOD
<ul>
<li>1</li>
<li>abc</li>
<li><></li>
</ul>
EOD;
$this
->
assertEqualsWithoutLE
(
$expected
,
Html
::
ul
(
$data
));
$expected
=
<<<EOD
<ul class="test">
<li class="item-0">1</li>
<li class="item-1">abc</li>
<li class="item-2"><></li>
</ul>
EOD;
$this
->
assertEqualsWithoutLE
(
$expected
,
Html
::
ul
(
$data
,
array
(
'class'
=>
'test'
,
'item'
=>
function
(
$index
,
$item
)
{
return
"<li class=
\"
item-
$index
\"
>
$item
</li>"
;
}
)));
}
public
function
testOl
()
{
$data
=
array
(
1
,
'abc'
,
'<>'
,
);
$expected
=
<<<EOD
<ol>
<li>1</li>
<li>abc</li>
<li><></li>
</ol>
EOD;
$this
->
assertEqualsWithoutLE
(
$expected
,
Html
::
ol
(
$data
));
$expected
=
<<<EOD
<ol class="test">
<li class="item-0">1</li>
<li class="item-1">abc</li>
<li class="item-2"><></li>
</ol>
EOD;
$this
->
assertEqualsWithoutLE
(
$expected
,
Html
::
ol
(
$data
,
array
(
'class'
=>
'test'
,
'item'
=>
function
(
$index
,
$item
)
{
return
"<li class=
\"
item-
$index
\"
>
$item
</li>"
;
}
)));
}
public
function
testRenderOptions
()
{
$data
=
array
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment