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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

passing php variable to template


Reply

5 Posts   2052 Views

Avatar
servalman

23 June 2010 at 5:57am Community Member, 211 Posts

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
banal

23 June 2010 at 8:45am (Last edited: 23 June 2010 8:46am), Community Member, 901 Posts

Hi

I usually use [url=http://code.google.com/p/swfobject/]SWF Object[/url] 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

23 June 2010 at 8:47pm Community Member, 211 Posts

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

24 June 2010 at 2:55am 4085 Posts

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

24 June 2010 at 3:45am Community Member, 211 Posts

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