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.

Template Questions /

Arguments to control function


Go to End
Reply


13 Posts   3927 Views

Avatar
JuLo

Community Member, 37 Posts

17 December 2009 at 2:12am

Edited: 17/12/2009 2:39am

Hi,

I've been trying to make a simple Javascript slideshow and I want my user to choose the folder to get the pictures from.

Here is what I did: (removed unnecessary code)

In Page. php

   public static $has_one = array(
      'FolderForFadeBanner' => 'Folder'
   );

   function getCMSFields() {
      $fields = parent::getCMSFields();   
      $fields->addFieldToTab("Root.Content.Banner", new TreeDropdownField("FolderForFadeBannerID", "Choose a folder", "Folder"));
   }

class Page_Controller extends ContentController {

   function returnimages($dirname = "../assets/images/Homepage") {
   (...)
      if($handle = opendir($dirname)) {
         while(false !== ($file = readdir($handle))){
   (...)

In Page.ss

<script type="text/javascript">
(...)
   imagearray: [ $returnimages],
(...)
</script>

This code works, because I obviously have declared a default value to be used with the returnimages() function.
My problem is that I want returnimages() to use the value collected from the CMS $FolderForFadeBanner.
I tried things like $returnimages($FolderForBanner) or <% control $returnimages($FolderForBanner) %> $files <% end_control %> in the ss file or declaring the function $returnimages($FolderForBanner) in the php file, but all to no avail.

At best, my slideshow works with the default folder.
At worst, I get an error, saying that I do not have enough arguments for returnimages.

Help please someone?

I know I am probably doing something really stupid, but I can't figure it out.

Thanks,

JuLo

[EDIT: Removed some more code that was not necessary. My post is still too long. Sorry. ]

Avatar
Willr

Forum Moderator, 5513 Posts

17 December 2009 at 9:00am

<% control $returnimages($FolderForBanner) %>

The template engine doesn't support passing variables into functions. You'll have to work out a different way to implement what you want to do.

Avatar
JuLo

Community Member, 37 Posts

17 December 2009 at 11:24am

OK. That's very disappointing.

What about accessing the FolderForFadeBanner from within the function? I mean, they are both declared in the same file. Can't I access it like it was a global variable or something?

And why did you say <% control $returnimages($FolderForBanner) %>?
What can I do with this?

Thanks,

Avatar
Willr

Forum Moderator, 5513 Posts

17 December 2009 at 11:37am

What about accessing the FolderForFadeBanner from within the function? I mean, they are both declared in the same file. Can't I access it like it was a global variable or something?

Yes you can access the variable using $this->VariableName which gets the db field, or method VariableName.

And why did you say <% control $returnimages($FolderForBanner) %>?

That was just a quote to show what you can't do.

Avatar
JuLo

Community Member, 37 Posts

17 December 2009 at 1:04pm

Edited: 17/12/2009 1:43pm

Hi again and thanks for your help,

I am however a bit confused.
Isn't using "<% control $returnimages($FolderForBanner) %>" just like passing variables into functions? (which you said cannot be done)
Would the usage be something like
<% control $returnimages($FolderForBanner) %>
$files //where files is a variable used in the returnimages(path) function
<% end_control %>

And if I were to use the $this->FolderForBanner method, it returns a Folder Object.
How can I use it with opendir() , which requires a string argument?

How to get the $FolderForBanner from its ID ($this->FolderForBannerID)?

Thanks again,

JuLo

Avatar
Willr

Forum Moderator, 5513 Posts

17 December 2009 at 2:09pm

That does return a Folder Object, if you want to call the link to the file have alook at the API for File - http://api.silverstripe.org/sapphire/filesystem/File.html for a method which returns the path. Think you need to use something like getFilename() will have the path - /assets/Uploads/File.html

Yes, $this-> FolderForFadeBannerID will get you the ID of the folder since its as has_one the fieldname has the ID appended to it.

Avatar
JuLo

Community Member, 37 Posts

17 December 2009 at 2:25pm

Thanks.

Sorry to hassle you, but I still don't know how to get to the $FolderForBanner from its ID.

$this->FolderForBanner is not set, while $this->FolderForBannerID is.

Should it be something like FolderForBanner($this->FolderForBannerID)?

In any case, thanks a lot for that link to the API.
I've been using Silverstripe for a couple of weeks and did not see it before.
I was only guessing so far... ;-)
FYI the function I will use will be getRelativePath()

Avatar
Willr

Forum Moderator, 5513 Posts

17 December 2009 at 2:28pm

Ah perhaps try $this->FolderForBanner() to return the Folder Object, if that doesn't work $this->dbObject('FolderForBanner'); should as well.

Go to Top