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   3872 Views

Avatar
JuLo

17 December 2009 at 2:12am (Last edited: 17 December 2009 2:39am), Community Member, 37 Posts

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

17 December 2009 at 9:00am Forum Moderator, 5511 Posts

<% 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

17 December 2009 at 11:24am Community Member, 37 Posts

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

17 December 2009 at 11:37am Forum Moderator, 5511 Posts

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

17 December 2009 at 1:04pm (Last edited: 17 December 2009 1:43pm), Community Member, 37 Posts

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

17 December 2009 at 2:09pm Forum Moderator, 5511 Posts

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

17 December 2009 at 2:25pm Community Member, 37 Posts

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

17 December 2009 at 2:28pm Forum Moderator, 5511 Posts

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

Go to Top