Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Template variables in Page.php

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

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

Page: 1
Go to End
Author Topic: 2186 Views
  • gakenny
    Avatar
    Community Member
    153 Posts

    Template variables in Page.php Link to this post

    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

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Template variables in Page.php Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Template variables in Page.php Link to this post

    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

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Template variables in Page.php Link to this post

    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);
    }

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Template variables in Page.php Link to this post

    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

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