Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Referencing theme files in PHP files

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1707 Views
  • Romeo
    Avatar
    Community Member
    67 Posts

    Referencing theme files in PHP files Link to this post

    This is driving me crazy...Earlier today I came across a handy variable which I can use in my Page.php etc. which gives me the path to the theme currently in use, to save having to hard code this stuff in there (always a bad idea). But for the life of me I can't find it again although I've searched through the docs for ages. Someone please put me out of my misery!

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Referencing theme files in PHP files Link to this post

    Within a template you can use $ThemeDir which is where you would likely be referencing things relatively.

    If you want to generate it yourself have a look at the code for the control above in ViewableData:

       public function ThemeDir($subtheme = null) {
          $theme = SSViewer::current_theme();
          if($theme) {
             return "themes/$theme" . ($subtheme ? "_$subtheme" : "");
          } else {
             return project();
          }
       }

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: Referencing theme files in PHP files Link to this post

    Thanks for that. Now knowing its name, I can determine that where I read about it was here: http://doc.silverstripe.org/doku.php?id=themes:developing. Should have thought to look there, but I'm still not sure what documentation is where.

  • neilcreagh
    Avatar
    Community Member
    94 Posts

    Re: Referencing theme files in PHP files Link to this post

    But what about referencing the theme directory on a PHP code page (as opposed to on a .ss theme page). Can $ThemeDir be used there instead of having to hardcode the reference to the chosen theme?

    For example to customise the icon for a new page type:

    static $icon = "themes/My_Theme_Name/images/treeicons/home";


    Is there a way to use $ThemeDir here so that the theme isn't hardcoded and if the theme changes this doesn't need to be updated?
    Thanks, Neil

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Referencing theme files in PHP files Link to this post

    this might work...

    function init() {
    parent::init();
    $this->set_stat('icon',"themes/".SSViewer::current_theme()."/images/treeicons/home");
    }

  • neilcreagh
    Avatar
    Community Member
    94 Posts

    Re: Referencing theme files in PHP files Link to this post

    That's not working for me. Thanks for the speedy response though

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Referencing theme files in PHP files Link to this post

    Just out of cursorily what does "doesn't work" mean?

    is the line of code called?
    if not then the init function is in the wrong place...
    if so does it attempt to set the static with the correct string?

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