Browse Source

Merge pull request #2992 from markstory/3.0-form-create-templates

3.0 form create templates
José Lorenzo Rodríguez 12 years ago
parent
commit
d20beed0e7

+ 32 - 5
src/View/Helper/FormHelper.php

@@ -256,6 +256,10 @@ class FormHelper extends Helper {
  * - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
  *    you should leave 'action' undefined.
  * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
+ * - `templates` The templates you want to use for this form. Any templates will be merged on top of
+ *   the already loaded templates. This option can either be a filename in App/Config that contains
+ *   the templates you want to load, or an array of templates to use. You can use
+ *   resetTemplates() to restore the original templates.
  * - `context` Additional options for the context class. For example the EntityContext accepts a 'table'
  *   option that allows you to set the specific Table class the form should be based on.
  * - `idPrefix` Prefix for generated ID attributes.
@@ -284,10 +288,20 @@ class FormHelper extends Helper {
 			'action' => null,
 			'url' => null,
 			'encoding' => strtolower(Configure::read('App.encoding')),
-			'idPrefix' => null
+			'templates' => null,
+			'idPrefix' => null,
 		];
 
 		$this->_idPrefix = $options['idPrefix'];
+		$templater = $this->getTemplater();
+
+		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']);
+
 		$action = $this->url($this->_formUrl($context, $options));
 		unset($options['url'], $options['action'], $options['idPrefix']);
 
@@ -326,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;
 	}
 
@@ -2218,6 +2232,19 @@ class FormHelper extends Helper {
 	}
 
 /**
+ * Restores the default values built into FormHelper.
+ *
+ * This method will not reset any templates set in custom widgets.
+ *
+ * @return void
+ */
+	public function resetTemplates() {
+		$reflection = new \ReflectionClass($this);
+		$properties = $reflection->getDefaultProperties();
+		$this->templates($properties['_defaultTemplates']);
+	}
+
+/**
  * Event listeners.
  *
  * @return array

+ 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);
 	}

+ 53 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -338,6 +338,46 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test create() with the templates option.
+ *
+ * @return void
+ */
+	public function testCreateTemplatesArray() {
+		$result = $this->Form->create($this->article, [
+			'templates' => [
+				'formstart' => '<form class="form-horizontal"{{attrs}}>',
+			]
+		]);
+		$expected = [
+			'form' => [
+				'class' => 'form-horizontal',
+				'method' => 'post',
+				'action' => '/articles/add',
+				'accept-charset' => 'utf-8'
+			]
+		];
+		$this->assertTags($result, $expected);
+	}
+
+/**
+ * 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
@@ -5748,4 +5788,17 @@ class FormHelperTest extends TestCase {
 		$this->assertTags($result, $expected);
 	}
 
+/**
+ * Test resetting templates.
+ *
+ * @return void
+ */
+	public function testResetTemplates() {
+		$this->Form->templates(['input' => '<input>']);
+		$this->assertEquals('<input>', $this->Form->getTemplater()->get('input'));
+
+		$this->assertNull($this->Form->resetTemplates());
+		$this->assertNotEquals('<input>', $this->Form->getTemplater()->get('input'));
+	}
+
 }

+ 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>'
+];