Browse Source

Allow specifying option 'block' => true to get same funtionality as `inline` => false for helpers.

ADmad 12 years ago
parent
commit
2ebf1d123f

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

@@ -1377,7 +1377,8 @@ class FormHelper extends Helper {
  * - `data` - Array with key/value to pass in input hidden
  * - `method` - Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'.
  * - `confirm` - Can be used instead of $confirmMessage.
- * - `block` - Choose a custom block to append the form tag to.
+ * - `block` - Set to true to append form to view block "postLink" or provide
+ *   custom block name.
  * - Other options are the same of HtmlHelper::link() method.
  * - The option `onclick` will be replaced.
  *
@@ -1431,6 +1432,9 @@ class FormHelper extends Helper {
 		$out .= $this->formatTemplate('formend', []);
 
 		if ($options['block']) {
+			if ($options['block'] === true) {
+				$options['block'] = __FUNCTION__;
+			}
 			$this->_View->append($options['block'], $out);
 			$out = '';
 		}

+ 30 - 5
src/View/Helper/HtmlHelper.php

@@ -183,13 +183,18 @@ class HtmlHelper extends Helper {
  *
  * `$this->Html->meta('icon', 'favicon.ico');
  *
+ * Append the meta tag to custom view block "meta":
+ *
+ * `$this->Html->meta('description', 'A great page', array('block' => true));`
+ *
  * Append the meta tag to custom view block:
  *
  * `$this->Html->meta('description', 'A great page', array('block' => 'metaTags'));`
  *
  * ### Options
  *
- * - `block` Choose a block to append the meta tag to.
+ * - `block` - Set to true to append output to view block "meta" or provide
+ *   custom block name.
  *
  * @param string $type The title of the external resource
  * @param string|array $url The address of the external resource or string for content attribute
@@ -255,6 +260,9 @@ class HtmlHelper extends Helper {
 		if (empty($options['block'])) {
 			return $out;
 		}
+		if ($options['block'] === true) {
+			$options['block'] = __FUNCTION__;
+		}
 		$this->_View->append($options['block'], $out);
 	}
 
@@ -356,13 +364,18 @@ class HtmlHelper extends Helper {
  *
  * `echo $this->Html->css(array('one.css', 'two.css'));`
  *
+ * Add the stylesheet to view block "css":
+ *
+ * `$this->Html->css('styles.css', array('block' => true));`
+ *
  * Add the stylesheet to a custom block:
  *
  * `$this->Html->css('styles.css', array('block' => 'layoutCss'));`
  *
  * ### Options
  *
- * - `block` Set the name of the block link/style tag will be appended to.
+ * - `block` Set to true to append output to view block "css" or provide
+ *   custom block name.
  * - `plugin` False value will prevent parsing path as a plugin
  * - `rel` Defaults to 'stylesheet'. If equal to 'import' the stylesheet will be imported.
  * - `fullBase` If true the URL will get a full address for the css file.
@@ -423,6 +436,9 @@ class HtmlHelper extends Helper {
 		if (empty($options['block'])) {
 			return $out;
 		}
+		if ($options['block'] === true) {
+			$options['block'] = __FUNCTION__;
+		}
 		$this->_View->append($options['block'], $out);
 	}
 
@@ -449,7 +465,8 @@ class HtmlHelper extends Helper {
  *
  * ### Options
  *
- * - `block` The name of the block you want the script appended to. Leave undefined to output inline.
+ * - `block` Set to true to append output to view block "script" or provide
+ *   custom block name.
  * - `once` Whether or not the script should be checked for uniqueness. If true scripts will only be
  *   included once, use false to allow the same script to be included more than once per request.
  * - `plugin` False value will prevent parsing path as a plugin
@@ -491,6 +508,9 @@ class HtmlHelper extends Helper {
 		if (empty($options['block'])) {
 			return $out;
 		}
+		if ($options['block'] === true) {
+			$options['block'] = __FUNCTION__;
+		}
 		$this->_View->append($options['block'], $out);
 	}
 
@@ -500,7 +520,8 @@ class HtmlHelper extends Helper {
  * ### Options
  *
  * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
- * - `block` Which block you want this script block appended to.
+ * - `block` Set to true to append output to view block "script" or provide
+ *   custom block name.
  *
  * @param string $script The script to wrap
  * @param array $options The options to use. Options not listed above will be
@@ -523,6 +544,9 @@ class HtmlHelper extends Helper {
 		if (empty($options['block'])) {
 			return $out;
 		}
+		if ($options['block'] === true) {
+			$options['block'] = 'script';
+		}
 		$this->_View->append($options['block'], $out);
 	}
 
@@ -534,7 +558,8 @@ class HtmlHelper extends Helper {
  * ### Options
  *
  * - `safe` Whether the code block should contain a CDATA
- * - `block` View block the output should be appended to
+ * - `block` Set to true to append output to view block "script" or provide
+ *   custom block name.
  *
  * @param array $options Options for the code block.
  * @return void

+ 2 - 2
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5046,7 +5046,7 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testPostLinkFormBuffer() {
-		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'postLink'));
+		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
 		$this->assertTags($result, array(
 			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
 			'Delete',
@@ -5064,7 +5064,7 @@ class FormHelperTest extends TestCase {
 		));
 
 		$result = $this->Form->postLink('Delete', '/posts/delete/2',
-			array('block' => 'postLink', 'method' => 'DELETE')
+			array('block' => true, 'method' => 'DELETE')
 		);
 		$this->assertTags($result, array(
 			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),

+ 33 - 5
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -560,8 +560,17 @@ class HtmlHelperTest extends TestCase {
 
 		$this->View->expects($this->at(0))
 			->method('append')
+			->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
+
+		$this->View->expects($this->at(1))
+			->method('append')
 			->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
-		$this->Html->css('more_css_in_head.css', array('block' => 'css'));
+
+		$result = $this->Html->css('css_in_head', array('block' => true));
+		$this->assertNull($result);
+
+		$result = $this->Html->css('more_css_in_head', array('block' => true));
+		$this->assertNull($result);
 
 		$result = $this->Html->css('screen', array('rel' => 'import'));
 		$expected = array(
@@ -598,6 +607,12 @@ class HtmlHelperTest extends TestCase {
 			'/style'
 		);
 		$this->assertTags($result, $expected);
+
+		$result = $this->Html->css('css_in_head', null, array('block' => true));
+		$this->assertNull($result);
+
+		$result = $this->Html->css('more_css_in_head', null, array('block' => true));
+		$this->assertNull($result);
 	}
 
 /**
@@ -960,8 +975,15 @@ class HtmlHelperTest extends TestCase {
 	public function testScriptWithBlocks() {
 		$this->View->expects($this->at(0))
 			->method('append')
+			->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
+
+		$this->View->expects($this->at(1))
+			->method('append')
 			->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
 
+		$result = $this->Html->script('script_in_head', array('block' => true));
+		$this->assertNull($result);
+
 		$result = $this->Html->script('second_script', array('block' => 'headScripts'));
 		$this->assertNull($result);
 	}
@@ -1057,8 +1079,15 @@ class HtmlHelperTest extends TestCase {
 
 		$this->View->expects($this->at(0))
 			->method('append')
+			->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
+
+		$this->View->expects($this->at(1))
+			->method('append')
 			->with('scriptTop', $this->stringContains('alert('));
 
+		$result = $this->Html->scriptBlock('window.foo = 2;', array('block' => true));
+		$this->assertNull($result);
+
 		$result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
 		$this->assertNull($result);
 
@@ -1119,8 +1148,7 @@ class HtmlHelperTest extends TestCase {
 
 		$this->View->expects($this->once())
 			->method('append');
-
-		$result = $this->Html->scriptStart(array('safe' => false, 'block' => 'scripts'));
+		$result = $this->Html->scriptStart(array('safe' => false, 'block' => true));
 		$this->assertNull($result);
 		echo 'this is some javascript';
 
@@ -1581,13 +1609,13 @@ class HtmlHelperTest extends TestCase {
 	public function testMetaWithBlocks() {
 		$this->View->expects($this->at(0))
 			->method('append')
-			->with('metas', $this->stringContains('ROBOTS'));
+			->with('meta', $this->stringContains('ROBOTS'));
 
 		$this->View->expects($this->at(1))
 			->method('append')
 			->with('metaTags', $this->stringContains('favicon.ico'));
 
-		$result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('block' => 'metas'));
+		$result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('block' => true));
 		$this->assertNull($result);
 
 		$result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));