Browse Source

Fix template files not being usable with FormHelper::create()

Mark Story 12 years ago
parent
commit
b07ed689a8

+ 9 - 6
src/View/Helper/FormHelper.php

@@ -293,9 +293,12 @@ class FormHelper extends Helper {
 		];
 
 		$this->_idPrefix = $options['idPrefix'];
+		$templater = $this->getTemplater();
 
-		if (!empty($options['templates'])) {
-			$this->templates($options['templates']);
+		if (!empty($options['templates']) && is_array($options['templates'])) {
+			$templater->add($options['templates']);
+		} elseif (!empty($options['templates']) && is_string($options['templates'])) {
+			$templater->load($options['templates']);
 		}
 		unset($options['templates']);
 
@@ -337,11 +340,11 @@ class FormHelper extends Helper {
 		}
 
 		if (!empty($append)) {
-			$append = $this->formatTemplate('hiddenblock', ['content' => $append]);
+			$append = $templater->format('hiddenblock', ['content' => $append]);
 		}
-		$actionAttr = $this->_templater->formatAttributes(['action' => $action, 'escape' => false]);
-		return $this->formatTemplate('formstart', [
-			'attrs' => $this->_templater->formatAttributes($htmlAttributes) . $actionAttr
+		$actionAttr = $templater->formatAttributes(['action' => $action, 'escape' => false]);
+		return $templater->format('formstart', [
+			'attrs' => $templater->formatAttributes($htmlAttributes) . $actionAttr
 		]) . $append;
 	}
 

+ 1 - 6
src/View/StringTemplate.php

@@ -70,12 +70,7 @@ class StringTemplate {
  * @return void
  */
 	public function load($file) {
-		list($plugin, $file) = pluginSplit($file);
-		$path = APP . 'Config/';
-		if ($plugin !== null) {
-			$path = Plugin::path($plugin) . 'Config/';
-		}
-		$loader = new PhpConfig($path);
+		$loader = new PhpConfig(APP . 'Config/');
 		$templates = $loader->read($file);
 		$this->add($templates);
 	}

+ 19 - 1
tests/TestCase/View/Helper/FormHelperTest.php

@@ -342,7 +342,7 @@ class FormHelperTest extends TestCase {
  *
  * @return void
  */
-	public function testCreateTemplates() {
+	public function testCreateTemplatesArray() {
 		$result = $this->Form->create($this->article, [
 			'templates' => [
 				'formstart' => '<form class="form-horizontal"{{attrs}}>',
@@ -360,6 +360,24 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test create() with the templates option.
+ *
+ * @return void
+ */
+	public function testCreateTemplatesFile() {
+		$result = $this->Form->create($this->article, [
+			'templates' => 'htmlhelper_tags.php',
+		]);
+		$expected = [
+			'start form',
+			'div' => ['class' => 'hidden'],
+			'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
+			'/div'
+		];
+		$this->assertTags($result, $expected);
+	}
+
+/**
  * test the create() method
  *
  * @dataProvider requestTypeProvider

+ 5 - 7
tests/test_app/TestApp/Config/htmlhelper_tags.php

@@ -1,9 +1,7 @@
 <?php
 
-$config = array(
-	'tags' => array(
-		'form' => 'start form',
-		'formend' => 'finish form',
-		'hiddenblock' => '<div class="hidden">%s</div>'
-	)
-);
+$config = [
+	'formstart' => 'start form',
+	'formend' => 'finish form',
+	'hiddenblock' => '<div class="hidden">{{content}}</div>'
+];