Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Arguments to control function

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

Page: 1 2
Go to End
Author Topic: 3676 Views
  • JuLo
    Avatar
    Community Member
    37 Posts

    Arguments to control function Link to this post

    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. ]

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Arguments to control function Link to this post

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

  • JuLo
    Avatar
    Community Member
    37 Posts

    Re: Arguments to control function Link to this post

    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,

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Arguments to control function Link to this post

    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.

  • JuLo
    Avatar
    Community Member
    37 Posts

    Re: Arguments to control function Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Arguments to control function Link to this post

    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.

  • JuLo
    Avatar
    Community Member
    37 Posts

    Re: Arguments to control function Link to this post

    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()

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Arguments to control function Link to this post

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

    3676 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.