Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Passing $URLSegment to PageController?

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

Page: 1
Go to End
Author Topic: 3402 Views
  • me.yay
    Avatar
    Community Member
    14 Posts

    Passing $URLSegment to PageController? Link to this post

    Hi,

    i am kind of desperate.. I am unsing a Gallery and want do determine the filenames for the Images based on the $URLSegment.
    The Pictures are located inside /themes/path/img/gallery and are named like the matching $URLSegment's with trailing numbers. The images are uploaded straight in the folder and not proccessed as assets.

    My first approach was to pass $URLSegment within the control, which failed:
    page.ss:

    <% control GalleryImages($URLSegment) %>
    $Imagename
    <% end_control %>

    Then i found in the docs that $URLSegment should be an accessible variable inside the Page_Controller class (and subclasses).
    This is my next approach:

    page.php:

       function GalleryImages(){
       
          $path = "../themes/path/img/gallery";
          $filetypes = array("jpg", "png", "gif");
          
          $files = new array();
          $handle=opendir ($path);
          while ($file = readdir ($handle)) {
           if (strlen($file) >= strlen($URLSegment)) {
             // filename starts with URLSegment?
              $matchfile= strpos($file, $URLSegment,0 ) === 0;
                // filename matches filetypes?
                foreach ($filetypes as $filetype){
                   $matchfiletype=substr_compare($file, $filetype, ((int) strlen($filetype) * -1)) === 0;
                   if ($matchfiletype && $matchfile) {
                      $files[] = array('Image' => $file);
                   }
                }
             }
          }
          closedir($handle);
          return new DataObjectSet($files);
       }

    What am i doing wrong? And how can i determin the current URLSegment inside the controller?

    Kind Regards
    Metin

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Passing $URLSegment to PageController? Link to this post

    how can i determin the current URLSegment inside the controller?

    $this->URLSegment will return the controllers URL segment

  • me.yay
    Avatar
    Community Member
    14 Posts

    Re: Passing $URLSegment to PageController? Link to this post

    Thanks for the quick hint Willr!

    It works like a charm

    3402 Views
Page: 1
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.