Skip to content

Commit

Permalink
Allow specifying option 'block' => true to get same funtionality as `…
Browse files Browse the repository at this point in the history
…inline` => false for helpers.
  • Loading branch information
ADmad committed Mar 11, 2014
1 parent 5df6c15 commit 2ebf1d1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/View/Helper/FormHelper.php
Expand Up @@ -1377,7 +1377,8 @@ public function postButton($title, $url, $options = array()) {
* - `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.
*
Expand Down Expand Up @@ -1431,6 +1432,9 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
$out .= $this->formatTemplate('formend', []);

if ($options['block']) {
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
$out = '';
}
Expand Down
35 changes: 30 additions & 5 deletions src/View/Helper/HtmlHelper.php
Expand Up @@ -183,13 +183,18 @@ public function docType($type = 'html5') {
*
* `$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
Expand Down Expand Up @@ -255,6 +260,9 @@ public function meta($type, $url = null, $options = array()) {
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
}

Expand Down Expand Up @@ -356,13 +364,18 @@ public function link($title, $url = null, $options = array(), $confirmMessage =
*
* `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.
Expand Down Expand Up @@ -423,6 +436,9 @@ public function css($path, $options = array()) {
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
}

Expand All @@ -449,7 +465,8 @@ public function css($path, $options = array()) {
*
* ### 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
Expand Down Expand Up @@ -491,6 +508,9 @@ public function script($url, $options = array()) {
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
}

Expand All @@ -500,7 +520,8 @@ public function script($url, $options = array()) {
* ### 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
Expand All @@ -523,6 +544,9 @@ public function scriptBlock($script, $options = array()) {
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = 'script';
}
$this->_View->append($options['block'], $out);
}

Expand All @@ -534,7 +558,8 @@ public function scriptBlock($script, $options = array()) {
* ### 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
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -5046,7 +5046,7 @@ public function testPostLinkAfterGetForm() {
* @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',
Expand All @@ -5064,7 +5064,7 @@ public function testPostLinkFormBuffer() {
));

$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;/'),
Expand Down
38 changes: 33 additions & 5 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -559,9 +559,18 @@ public function testCssLink() {
$this->assertEquals(2, count($result));

$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(
Expand Down Expand Up @@ -598,6 +607,12 @@ public function testCssLinkBC() {
'/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);
}

/**
Expand Down Expand Up @@ -959,9 +974,16 @@ public function testPluginScript() {
*/
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);
}
Expand Down Expand Up @@ -1056,9 +1078,16 @@ public function testScriptBlock() {
$this->assertTags($result, $expected);

$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);

Expand Down Expand Up @@ -1119,8 +1148,7 @@ public function testScriptStartAndScriptEnd() {

$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';

Expand Down Expand Up @@ -1581,13 +1609,13 @@ public function testMetaIcon() {
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'));
Expand Down

0 comments on commit 2ebf1d1

Please sign in to comment.