Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add block option to HtmlHelper::scriptBlock()

  • Loading branch information...
commit 94778ef673739c3be4f9df74c78ac4ee16710dad 1 parent edeca2f
@markstory markstory authored
View
9 lib/Cake/Test/Case/View/Helper/HtmlHelperTest.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'),
View
26 lib/Cake/View/Helper/HtmlHelper.php
@@ -565,11 +565,15 @@ 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()) {
@@ -577,14 +581,18 @@ public function scriptBlock($script, $options = array()) {
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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.