Skip to content

Commit de15f68

Browse files
committed
Reduce the number of templates used.
By adding {{attrs}} I can re-add support for arbitrary properties on option elements. As well as the [name => foo, value => bar] format for option elements.
1 parent a2d7f8d commit de15f68

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

Cake/View/Input/SelectBox.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ protected function _renderOptions($options, $disabled, $selected, $escape) {
137137
'content' => implode('', $groupOptions)
138138
]);
139139
} else {
140-
$template = 'option';
141-
$isSelected = $this->_isSelected($key, $selected);
142-
$isDisabled = $this->_isDisabled($key, $disabled);
143-
if ($isSelected) {
144-
$template .= 'Selected';
140+
$optAttrs = [];
141+
if ($this->_isSelected($key, $selected)) {
142+
$optAttrs['selected'] = true;
145143
}
146-
if ($isDisabled) {
147-
$template .= 'Disabled';
144+
if ($this->_isDisabled($key, $disabled)) {
145+
$optAttrs['disabled'] = true;
148146
}
147+
$optAttrs['escape'] = $escape;
149148

150-
$out[] = $this->_templates->format($template, [
149+
$out[] = $this->_templates->format('option', [
151150
'name' => $escape ? h($key) : $key,
152151
'value' => $escape ? h($val) : $val,
152+
'attrs' => $this->_templates->formatAttributes($optAttrs),
153153
]);
154154
}
155155
}

Test/TestCase/View/Input/SelectBoxTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public function setUp() {
2727
parent::setUp();
2828
$templates = [
2929
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
30-
'option' => '<option value="{{name}}">{{value}}</option>',
31-
'optionSelected' => '<option value="{{name}}" selected="selected">{{value}}</option>',
32-
'optionDisabled' => '<option value="{{name}}" disabled="disabled">{{value}}</option>',
33-
'optionSelectedDisabled' => '<option value="{{name}}" selected="selected" disabled="disabled">{{value}}</option>',
30+
'option' => '<option value="{{name}}"{{attrs}}>{{value}}</option>',
3431
'optgroup' => '<optgroup label="{{label}}">{{content}}</optgroup>',
3532
];
3633
$this->templates = new StringTemplate();

0 commit comments

Comments
 (0)