Skip to content
This repository
Browse code

Add block option to HtmlHelper::scriptBlock()

  • Loading branch information...
commit 94778ef673739c3be4f9df74c78ac4ee16710dad 1 parent edeca2f
Mark Story authored
9  lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -790,13 +790,20 @@ public function testScriptBlock() {
790 790
 		$this->assertTags($result, $expected);
791 791
 
792 792
 
793  
-		$this->View->expects($this->any())
  793
+		$this->View->expects($this->at(0))
794 794
 			->method('append')
795 795
 			->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
796 796
 
  797
+		$this->View->expects($this->at(1))
  798
+			->method('append')
  799
+			->with('scriptTop', $this->stringContains('alert('));
  800
+
797 801
 		$result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
798 802
 		$this->assertNull($result);
799 803
 
  804
+		$result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
  805
+		$this->assertNull($result);
  806
+
800 807
 		$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
801 808
 		$expected = array(
802 809
 			'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
26  lib/Cake/View/Helper/HtmlHelper.php
@@ -565,11 +565,15 @@ public function script($url, $options = array()) {
565 565
  * ### Options
566 566
  *
567 567
  * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
568  
- * - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline
  568
+ * - `inline` (boolean) Whether or not the $script should be added to 
  569
+ *   `$scripts_for_layout` / `script` block, or output inline. (Deprecated, use `block` instead)
  570
+ * - `block` Which block you want this script block appended to.
  571
+ *   Defaults to `script`.
569 572
  *
570 573
  * @param string $script The script to wrap
571  
- * @param array $options The options to use.
572  
- * @return mixed string or null depending on the value of `$options['inline']`
  574
+ * @param array $options The options to use. Options not listed above will be 
  575
+ *    treated as HTML attributes.
  576
+ * @return mixed string or null depending on the value of `$options['block']`
573 577
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
574 578
  */
575 579
 	public function scriptBlock($script, $options = array()) {
@@ -577,14 +581,18 @@ public function scriptBlock($script, $options = array()) {
577 581
 		if ($options['safe']) {
578 582
 			$script  = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
579 583
 		}
580  
-		$inline = $options['inline'];
  584
+		if (!$options['inline'] && empty($options['block'])) {
  585
+			$options['block'] = 'script';
  586
+		}
581 587
 		unset($options['inline'], $options['safe']);
582  
-		$attributes = $this->_parseAttributes($options, ' ', ' ');
583  
-		if ($inline) {
584  
-			return sprintf($this->_tags['javascriptblock'], $attributes, $script);
  588
+
  589
+		$attributes = $this->_parseAttributes($options, array('block'), ' ');
  590
+		$out = sprintf($this->_tags['javascriptblock'], $attributes, $script);
  591
+
  592
+		if (empty($options['block'])) {
  593
+			return $out;
585 594
 		} else {
586  
-			$this->_View->append('script', sprintf($this->_tags['javascriptblock'], $attributes, $script));
587  
-			return null;
  595
+			$this->_View->append($options['block'], $out);
588 596
 		}
589 597
 	}
590 598
 

0 notes on commit 94778ef

Please sign in to comment.
Something went wrong with that request. Please try again.