Yii Framework Forum: Including dynamic javascript - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Including dynamic javascript Rate Topic: -----

#1 User is offline   SystemicPlural 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 12-April 10

Posted 07 September 2010 - 02:17 AM

I have a view that needs to display a few dynamic javascript variables in a script tag, they are then picked up by some included js files.

What is the best way of doing this?

Thanks for your help
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 07 September 2010 - 02:42 AM

<?php
Yii::app()->clientScript->registerScript(
    'initMyJavascript',   // Identifier for this snippet
    "myJSVar={val1: $something, val2: $somethingEls, val3: 'somethingStatic'};",
    CClientScript::POS_READY
); ?>


The last argument defines, where your code is inserted.

EDIT: Forgot the <?php ...

This post has been edited by Mike: 07 September 2010 - 02:42 AM

1

#3 User is offline   SystemicPlural 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 12-April 10

Posted 07 September 2010 - 02:44 AM

Thanks Mike, that's great.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users