0

I'm trying to use registerScript() to output some raw javascript into my page. The following seems to work fine (at the view level, called by a controller):

modules/myApp/views/readEditor.php:

<?php
$cs = Yii::app()->getClientScript();  
$cs->registerScript(
                'my-hello-world-1',
                'var someCoolVariableInView = 11;',
                CClientScript::POS_END
);
?>

This shows up before as it should:

/*<![CDATA[*/
var someCoolVariableInView = 11;
/*]]>*/

However, when I do the equivalent in a widget's view:

modules/myApp/components/views/myWidget.php:

<?php
$cs = Yii::app()->getClientScript();  
$cs->registerScript(
                'my-hello-world-2',
                'var someCoolVariableInWidget = 10;',
                CClientScript::POS_END
);
?>

The javascript does not show up.

I've done a bunch of googling but have not found anything helpful. My guess is the widget is filtering out the javascript...hopefully there's a parameter or config switch I can change to get this to output (or maybe there's a better way of doing this).

Thx,

Peter

UPDATE:

how widget is rendered:

<?php
class myWidget extends CWidget {

    public function init() {
    } // end init()

    public function run() {
        // ...
        $this->render('myWidget');
    } // end run()

}
PeterG
  • 772
  • 2
  • 11
  • 28
  • This "widget's view" looks highly suspicious. How is it rendered? – Jon Mar 04 '13 at 21:53
  • @Jon added more code, showing how widget is rendered. Is this what you meant? What's suspicious? Note I have divs and other content in the widget view that outputs fine, just the script doesn't show up. – PeterG Mar 04 '13 at 21:57
  • How are you loading the widget? – Alex Mar 05 '13 at 09:12

2 Answers2

0

Put your registerScript in init()

gnysek
  • 319
  • 1
  • 15
0

In your controller try the following code to work with processing of rendering javascript code.

$output=$this->widget('myWidget',array(),true); 
echo $this->processOutput($output);
josliber
  • 43,891
  • 12
  • 98
  • 133