7912 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » passing php variable to template
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1752 Views |
-
passing php variable to template

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;}
}
-
Re: passing php variable to template

23 June 2010 at 8:45am Last edited: 23 June 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..
-
Re: passing php variable to template

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
-
Re: passing php variable to template

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 %> -
Re: passing php variable to template

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.jpgmais cela ne marche pas mon image n'est pas chargée aurais tu une suggestion ?
Merci encore
| 1752 Views | ||
|
Page:
1
|
Go to Top |


