Jump to:

23355 Posts in 17734 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » passing silverstripe variables into inline javascript [SOLVED]

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4020 Views
  • slavelabourer
    Avatar
    Community Member
    25 Posts

    passing silverstripe variables into inline javascript [SOLVED] Link to this post

    Hi, i saw a couple of topics on this but I didn't really understand what was going on.

    basically I want to pass silverstripe variables into moogaloop(vimeo)

    <script type="text/javascript" src="mysite/javascript/swfobject.js"></script>
       <script>
          
          function playNewVideo()
          {
             alert("PlayNewVideo");
             var flashvars = {
             'clip_id': '5846276', // this should be a ss variable
             'server': 'vimeo.com',
             'show_title': 1,
             'show_byline': 1,
             'show_portrait': 0,
             'fullscreen': 1,
             'js_api': 1
             }

    Any hint s on where a newbie can start to figure this out would be greatly apperciated.

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    Something like this should work:

    class SomePage_Controller extends Page_Controller{
    function init(){
       parent::init();

       Requirements::javascript('mysite/javascript/swfobject.js');
       Requirements::customScript("
          var flashvars = {
          'clip_id': '".$this->someMethodToGetTheClipID()."', // this should be a ss variable
          'server': 'vimeo.com',
          'show_title': 1,
          'show_byline': 1,
          'show_portrait': 0,
          'fullscreen': 1,
          'js_api': 1
    }

       ");
       
    }
    }

    See this page for more info:

    http://doc.silverstripe.org/requirements

  • slavelabourer
    Avatar
    Community Member
    25 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    Cheers man that was pretty helpful. still some problems though.

    putting the script inside Requirements::customScript SS complains about a bunch of syntax errors so i put it in

    Requirements::javaScript("mysite/javascript/vimeo.js");

    but I still cannot pass the variables into the js

    VideoPage.php

    <?php

    class VideoPage extends SiteTree {
       
       public static $db = array(
       'MapURL' => 'HTMLText',
       'VimeoID' => 'Text',
       'Marker1' => 'VarChar',
       'Marker2' => 'VarChar',
       'Marker3' => 'VarChar',
       'mapContent' => 'HTMLText',
       'Date' => 'Date',
       'shortDescription' => 'Text',
       'Category' => "Enum('Cyclist, Driver,High Interaction, Low Interaction, Fast, Slow')"
       
       
       );
       
       public static $has_one = array(
          'Thumbnail' => 'Image'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Date', 'Date'),'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextAreaField('shortDescription', 'Short Description'));
          $fields->addFieldToTab('Root.Content.Main', new DropdownField(
          'Category',
          'Category',
          singleton('VideoPage')->dbObject('Category')->enumValues()
          ), 'Content');
          
          $fields->addFieldToTab('Root.Content.Main', new HtmlEditorField('mapContent', 'Map Content'));
          $fields->addFieldToTab('Root.Content.Embed', new TextareaField('MapURL', 'Google Map URL'));
          $fields->addFieldToTab('Root.Content.Embed', new TextField('VimeoID', 'Vimeo video ID'));
          $fields->addFieldToTab('Root.Content.Embed', new ImageField('Thumbnail', "Thumbnail"));
          $fields->addFieldToTab('Root.Content.Embed', new NumericField('Marker1', 'playhead Marker 1'));
          $fields->addFieldToTab('Root.Content.Embed', new NumericField('Marker2', 'playhead Marker 2'));
          $fields->addFieldToTab('Root.Content.Embed', new NumericField('Marker3', 'playhead Marker 3'));
          
          return $fields;
       }
       
    }

    class VideoPage_Controller extends Page_Controller {
       
       public function init() {
          parent::init();
          
          
          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
          //$query = DataObject::get("VideoPage", "ID = $this->VimeoID");

          
          Requirements::javaScript("mysite/javascript/vimeo.js");
          
             
       }
       public function doVimeo() {
          $query = (int)DB::query("SELECT VimeoID FROM `VideoPage`WHERE `ID` = '$this->ID' AND VimeoID IS NOT NULL")->value();
              $text = $query;
             
              
             //$query = ("VimeoURL FROM `videopage`WHERE `ID` = '$this->ID'");
             $result = mysql_query($query);
             $doc = new DomDocument('1.0');

          // create root node
             $root = $doc->createElement('vimeoURL');
             $root = $doc->appendChild($root);
             
              // add node for each row
              $occ = $doc->createElement('vimeoID');
              $occ = $root->appendChild($occ);
              $child = $doc->createElement('ID');
              $child = $occ->appendChild($child);
              $value = $doc->createTextNode('ID' + $query);
              $value = $child->appendChild($value);
             
          $xml_string = $doc->saveXML();
          //header('Content-Type: application/xml; charset=ISO-8859-1');
          //echo $xml_string;   
       }   
    }

    ?>


    vimeo.js

    function playNewVideo()
          {
          console.log('".$this->vimeoID()."');
             alert("PlayNewVideo");
             var flashvars = {
             'clip_id': '.$this->doVimeo().',
             'server': 'vimeo.com',
             'show_title': 1,
             'show_byline': 1,
             'show_portrait': 0,
             'fullscreen': 1,
             'js_api': 1
             }
          
             var parObj = {
                'swliveconnect':true,
                'fullscreen': 1,
                'allowscriptaccess': 'always',
                'allowfullscreen':true
             };
             
             var attObj = {}
             attObj.id="myFlashID";
             
             
             swfobject.embedSWF("http://www.vimeo.com/moogaloop.swf", "myContent", "100%", "195", "9.0.28", '',flashvars,parObj, attObj );
          }

    would it be better to use dataobject instead?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    You might be looking for Requirements::javascriptTemplate()

    You use it like this:

    Requirements::javascriptTemplate('mysite/javascript/vimeo.js', array(
    'VimeoID' => $this->VimeoID
    ));

    Then in the vimeo.js file you can reference $VimeoID or any other parameters you define in the array (second argument to javascriptTemplate).

    Hope that helps!

    Sean

  • slavelabourer
    Avatar
    Community Member
    25 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    awesome man, thats exactly what I needed.

    Thanks so much, I can mark this as sovled out

    peace out

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    This is a handy thread. And just in time as are so many I read!

    How about passing an array in as a variable? Is this possible?

    What I really want to do is call a method on my controller that returns an array of slideshow objects to use in a JS slideshow. When trying to pass the returned array as a variable my page throws the error: [Notice] Array to string conversion.

    My array contains a string indexed array for each slideshow item, as follows

    array(
    array(
    "image" => "1.jpg",
    "thumb" => "1-thumb.jpg",
    "clickURL" => "project-1.html"
    ),
    array(
    "image" => "2.jpg",
    "thumb" => "2-thumb.jpg",
    "clickURL" => "project-2.html"
    ),
    array(
    "image" => "3.jpg",
    "thumb" => "3-thumb.jpg",
    "clickURL" => "project-3.html"
    )
    );

    What's the best solution to implement this, if any?

    Thanks!

  • cuSSter
    Avatar
    Community Member
    56 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    Hi timwjohn,

    You are using AJAX in calling the function from your controller,right?

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: passing silverstripe variables into inline javascript [SOLVED] Link to this post

    Hi,
    No the script is being run on page load. It's just the JavaScript class takes an array of image objects, which would be specific to that instance of the page type. What I've done is create a method in the page controller that generates the script and that's called with by Requirements::customScript method.
    If you have any different ideas I'd be inerested to hear them.
    Cheers.

    4020 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.