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.

Archive /

Our old forums are still available as a read-only archive.

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

Template variables in Page.php


Reply


5 Posts   2299 Views

Avatar
gakenny

Community Member, 153 Posts

19 October 2007 at 9:30pm

Hello,

I want to define my own funtion in Page.php that will return true/false based on the existence of a file. This file is contained in the assets/images folder.

Questions:
(1) Are any of the template variables available in Page.php (ie. $URLSegment). This seems to be blank if I print this.
(2) What variable can I use to identify the base URL for the SilverStripe installation?

Here is what I would like my code to look like:
   function imageExists() {

      // Check that the image file exists for this page
      // in the specified path
      $filename = SSpath . 'assets/images/' . $URLSegment . '.jpg';
      return file_exists($filename);
   }

Can anyone help me with these two questions?

Thanks,

Gary

Avatar
gakenny

Community Member, 153 Posts

20 October 2007 at 9:20am

Hello,

I have been able to find the answer (1):
To address the templating variable $URLSegment in Page.php, use $this->URLSegment.

Is there any variable available in SilverStripe to find the base URL and the base path to the SilverStripe installation?

Cheers,

Gary

Avatar
Sean

Forum Moderator, 922 Posts

20 October 2007 at 1:30pm

Edited: 20/10/2007 1:33pm

Director::fileExists(project() . '/css/layout.css') - this checks if 'projectdir/css/layout.css' exists

Director::absoluteBaseURL() - returns the base URL of the current site.

:-)

Sean

Avatar
gakenny

Community Member, 153 Posts

20 October 2007 at 7:20pm

Edited: 20/10/2007 9:30pm

Thanks Sean, that worked a treat.

I used:
function imageExists() {

// Check that the image file exists for this page
// in the specified path
$filename = 'assets/images/' . $this->URLSegment . '.jpg';
return Director::fileExists($filename);
}

Avatar
Sean

Forum Moderator, 922 Posts

20 October 2007 at 8:45pm

Great.

After looking at the code more closely, it would appear Director::fileExists() uses the php function file_exists(), with the exception of not needing to give it the absolute URL of the file.

Feel free to post any other questions. :-)

Cheers,
Sean