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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

passing php variable to template


Go to End


5 Posts   2521 Views

Avatar
servalman

Community Member, 211 Posts

23 June 2010 at 5:57am

Hello

I've been trying for a while to add php variable to the <param name="flashvars" /> for the flash object.

I'm finally close to it as I can pass to the template the value returned by my flashvarFunction() that way :

<param name="flashvars" value=$flashvarFunction/>

This works fine but now I want to do the same thing based on the dataobjectmanager example with let's say the Author or Quote value.

I'm not a php coder so I find it hard bur I'm sure there is a way of returning those value

Can someone help me ?

Thanks a lot

Here is the code

<?
class TestimonialPage extends Page
{
static $has_many = array (
'Testimonials' => 'Testimonial'
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.Testimonials", new DataObjectManager(
$this,
'Testimonials',
'Testimonial',
array('Author'=>'Author','Quote' => 'Quote'),
'getCMSFields_forPopup'

));

return $f;
}

}

class TestimonialPage_Controller extends Page_Controller
{

function ShowTestimonials() {
return DataObject::get('Testimonials','Quote','Author');
}

function flashvarFunction() {

$flashVar="'myFlashVar=titopt'" ;
return $flashVar;

}

}

Avatar
bummzack

Community Member, 904 Posts

23 June 2010 at 8:45am

Edited: 23/06/2010 8:46am

Hi

I usually use SWF Object to embed flash content. There you can pass the variables via JavaScript and you can easily pass them from PHP by using the json_encode function.

Of course it also works fine without SWFObject:
Since plain flashvars are passed in a http query-string format, you could use the PHP http_build_query function (http://php.net/manual/en/function.http-build-query.php). So your function would look something like this:

function flashvarFunction(){
	$data = array(
		'titlevar' => $this->Title,
		'othervar' => 'Some other value?'
	);
	return http_build_query($data);
}

You can populate your $data array with anything you want..

Avatar
servalman

Community Member, 211 Posts

23 June 2010 at 8:47pm

hello

thanks for your answer

I will try this solution but my problem is that I don't know how to retrieve (see the code) the values of 'Quote' or 'Author'

for the template part it works with : return DataObject::get('Testimonials','Quote','Author');

but for my flashvarFunction() i don't know how to write something like :

$flashVar = $Quote

maybe I have to access the array but I don't know how to get it.

Please help

thank you

T

Avatar
UncleCheese

Forum Moderator, 4102 Posts

24 June 2010 at 2:55am

This is wrong:

return DataObject::get('Testimonials','Quote','Author');

The arguments for the get() function are $class, $filter, $sort, $join, $limit.. Not a list of all the fields you want.

In fact, on a TestimonialsPage, that function is not needed, because you can automatically get all the Testimonial object on the page with $this->Testimonials(). So lose that function.

All you need is to edit your Testimonial class, and add:

public function FlashVars() {
$vars = array (
'Quote' => $this->Quote,
'Author' => $this->Author,
'Something' => 'Else',
'Brie' => 'Fromage'
);
return http_build_query($vars);
}

Then on your template:

<% control Testimonials %>
Quote is $Quote
Author is $Author
<object src="some.swf?{$FlashVars}"></object>
<% end_control %>

Avatar
servalman

Community Member, 211 Posts

24 June 2010 at 3:45am

Merci encore et encore Oncle fromage !

Puis je te poser une autre question sur flash ? :

J'ai un drole de probleme dans un script je charge une image avec :

loader.load(new URLRequest("image_01.jpg"));

cela fonctionne tres bien en local car l'image et le swf sont dans le même dossier, donc dans le cms j'ai mis le swf et l'image dans le même dossier :assets/swf/flashmovie.swf
assets/swf/image_01.jpg

mais cela ne marche pas mon image n'est pas chargée aurais tu une suggestion ?

Merci encore