Browse Source

Don't call format() to build attributes.

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.
mark_story 11 years ago
parent
commit
cd5d039d56

+ 3 - 14
src/View/StringTemplate.php

@@ -47,8 +47,6 @@ class StringTemplate {
  * @var array
  */
 	protected $_defaultConfig = [
-		'attribute' => '{{name}}="{{value}}"',
-		'compactAttribute' => '{{name}}="{{value}}"',
 	];
 
 /**
@@ -251,26 +249,17 @@ class StringTemplate {
 			$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) . '"';
 	}
 
 }

+ 1 - 5
tests/TestCase/View/Helper/StringTemplateTraitTest.php

@@ -65,12 +65,10 @@ class StringTemplateTraitTest extends TestCase {
 
 		$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'
 		);
 	}
 
@@ -87,8 +85,6 @@ class StringTemplateTraitTest extends TestCase {
 
 		$this->assertEquals(
 			[
-				'attribute' => '{{name}}="{{value}}"',
-				'compactAttribute' => '{{name}}="{{value}}"',
 				'text' => '<p>{{text}}</p>'
 			],
 			$this->Template->templates(),