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.

General Questions

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

Referencing theme files in PHP files


7 Posts   1759 Views


27 August 2009 at 1:02am Community Member, 67 Posts

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!


27 August 2009 at 9:20am (Last edited: 27 August 2009 9:21am), Community Member, 283 Posts

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


27 August 2009 at 9:58am Community Member, 67 Posts

Thanks for that. Now knowing its name, I can determine that where I read about it was here: Should have thought to look there, but I'm still not sure what documentation is where.


24 May 2011 at 1:28am Community Member, 94 Posts

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


24 May 2011 at 1:54am Forum Moderator, 1796 Posts

this might work...

function init() {


24 May 2011 at 2:34am Community Member, 94 Posts

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


24 May 2011 at 2:41am Forum Moderator, 1796 Posts

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?