Skip to content

Commit

Permalink
Don't call format() to build attributes.
Browse files Browse the repository at this point in the history
This is just waste. I've yet to see any real reason to allow attribute
formatting to be templatable. Cutting this out saves numerous calls to
format() which can pile up and be expensive.
  • Loading branch information
markstory committed Aug 7, 2014
1 parent 2600c65 commit cd5d039
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
17 changes: 3 additions & 14 deletions src/View/StringTemplate.php
Expand Up @@ -47,8 +47,6 @@ class StringTemplate {
* @var array
*/
protected $_defaultConfig = [
'attribute' => '{{name}}="{{value}}"',
'compactAttribute' => '{{name}}="{{value}}"',
];

/**
Expand Down Expand Up @@ -251,26 +249,17 @@ protected function _formatAttribute($key, $value, $escape = true) {
$value = implode(' ', $value);
}
if (is_numeric($key)) {
return $this->format('compactAttribute', [
'name' => $value,
'value' => $value
]);
return "$value=\"$value\"";
}
$truthy = [1, '1', true, 'true', $key];
$isMinimized = in_array($key, $this->_compactAttributes);
if ($isMinimized && in_array($value, $truthy, true)) {
return $this->format('compactAttribute', [
'name' => $key,
'value' => $key
]);
return "$key=\"$key\"";
}
if ($isMinimized) {
return '';
}
return $this->format('attribute', [
'name' => $key,
'value' => $escape ? h($value) : $value
]);
return $key . '="' . ($escape ? h($value) : $value) . '"';
}

}
6 changes: 1 addition & 5 deletions tests/TestCase/View/Helper/StringTemplateTraitTest.php
Expand Up @@ -65,12 +65,10 @@ public function testInitStringTemplates() {

$this->assertEquals(
[
'attribute' => '{{name}}="{{value}}"',
'compactAttribute' => '{{name}}="{{value}}"',
'text' => '<p>{{text}}</p>'
],
$this->Template->templates(),
'newly added template should be inlcuded in template list'
'newly added template should be included in template list'
);
}

Expand All @@ -87,8 +85,6 @@ public function testInitStringTemplatesArrayForm() {

$this->assertEquals(
[
'attribute' => '{{name}}="{{value}}"',
'compactAttribute' => '{{name}}="{{value}}"',
'text' => '<p>{{text}}</p>'
],
$this->Template->templates(),
Expand Down

0 comments on commit cd5d039

Please sign in to comment.