Skip to content
Permalink
Browse files

Add block option to HtmlHelper::scriptBlock()

  • Loading branch information...
markstory committed Feb 1, 2012
1 parent edeca2f commit 94778ef673739c3be4f9df74c78ac4ee16710dad
Showing with 25 additions and 10 deletions.
  1. +8 −1 lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
  2. +17 −9 lib/Cake/View/Helper/HtmlHelper.php
@@ -790,13 +790,20 @@ public function testScriptBlock() {
$this->assertTags($result, $expected);
$this->View->expects($this->any())
$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('inline' => false));
$this->assertNull($result);
$result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
$this->assertNull($result);
$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
$expected = array(
'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
@@ -565,26 +565,34 @@ public function script($url, $options = array()) {
* ### Options
*
* - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
* - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline
* - `inline` (boolean) Whether or not the $script should be added to
* `$scripts_for_layout` / `script` block, or output inline. (Deprecated, use `block` instead)
* - `block` Which block you want this script block appended to.
* Defaults to `script`.
*
* @param string $script The script to wrap
* @param array $options The options to use.
* @return mixed string or null depending on the value of `$options['inline']`
* @param array $options The options to use. Options not listed above will be
* treated as HTML attributes.
* @return mixed string or null depending on the value of `$options['block']`
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
*/
public function scriptBlock($script, $options = array()) {
$options += array('safe' => true, 'inline' => true);
if ($options['safe']) {
$script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
}
$inline = $options['inline'];
if (!$options['inline'] && empty($options['block'])) {
$options['block'] = 'script';
}
unset($options['inline'], $options['safe']);
$attributes = $this->_parseAttributes($options, ' ', ' ');
if ($inline) {
return sprintf($this->_tags['javascriptblock'], $attributes, $script);
$attributes = $this->_parseAttributes($options, array('block'), ' ');
$out = sprintf($this->_tags['javascriptblock'], $attributes, $script);
if (empty($options['block'])) {
return $out;
} else {
$this->_View->append('script', sprintf($this->_tags['javascriptblock'], $attributes, $script));
return null;
$this->_View->append($options['block'], $out);
}
}

0 comments on commit 94778ef

Please sign in to comment.
You can’t perform that action at this time.