Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

passing silverstripe variables into inline javascript [SOLVED]


Go to End


9 Posts   10853 Views

Avatar
slavelabourer

Community Member, 26 Posts

10 September 2010 at 10:20am

Edited: 18/10/2010 4:35pm

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.

Avatar
Martijn

Community Member, 271 Posts

10 September 2010 at 11:41am

Edited: 10/09/2010 11:42am

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

Avatar
slavelabourer

Community Member, 26 Posts

18 October 2010 at 2:57pm

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?

Avatar
Sean

Forum Moderator, 922 Posts

18 October 2010 at 3:16pm

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

Avatar
slavelabourer

Community Member, 26 Posts

18 October 2010 at 4:34pm

awesome man, thats exactly what I needed.

Thanks so much, I can mark this as sovled out

peace out

Avatar
timwjohn

Community Member, 98 Posts

29 October 2010 at 2:26am

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!

Avatar
cuSSter

Community Member, 56 Posts

3 November 2010 at 1:44am

Hi timwjohn,

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

Avatar
timwjohn

Community Member, 98 Posts

3 November 2010 at 2:46am

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.

Go to Top