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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Referencing theme files in PHP files


Go to End


7 Posts   6560 Views

Avatar
Romeo

Community Member, 67 Posts

27 August 2009 at 1:02am

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!

Avatar
dalesaurus

Community Member, 283 Posts

27 August 2009 at 9:20am

Edited: 27/08/2009 9:21am

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

Avatar
Romeo

Community Member, 67 Posts

27 August 2009 at 9:58am

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.

Avatar
neilcreagh

Community Member, 136 Posts

24 May 2011 at 1:28am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

24 May 2011 at 1:54am

this might work...

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

Avatar
neilcreagh

Community Member, 136 Posts

24 May 2011 at 2:34am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

24 May 2011 at 2:41am

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?