aliased paths for current theme?

Topics: Writing themes
Jun 26, 2011 at 2:25 AM

I've noticed that items placed in a theme must be referenced using a full path to them, such as: /Themes/HandcraftedRed/Styles/Site.css

Is there any easy way that themes could redefine these absolutes so that more direct paths could be used? such as, the Syles folder be accessible direction as /Styles, and it use the folder accessible under whatever the currently selected theme is?

Seems this would make the html output quite a bit cleaner.


Jun 27, 2011 at 11:41 AM

There's a module that will achieve this for the Media folder:

Actually, it's implemented as a generic virtual directory mapper; so you could virtualise your theme folder directly in its settings; that only just occurred to me and I think I'll be doing that as well...

Jun 27, 2011 at 10:46 PM

Actually, absolute paths (starting with '/') should never be used, anywhere. They break the ability to run anywhere but on the root of a domain.

In stylesheets, relative paths (from the stylesheet path) should be used.

In views, you should use Url.Content.

Jun 27, 2011 at 11:34 PM

yes i realized i was trying to reference some theme-level images from a page-level content, so i was just doing things wrong.

however, it would still be nice to be able to hide the full theme path, so it doesnt link to /themes/some-weird-theme-name-i-installed/styles/site.css inside the page etc... seems like it reveals implementation details to the world that don't need to be there.

Jun 27, 2011 at 11:38 PM

You want static files to be served as fast as possible. If you point to their physical location, they can be served by IIS without even going into the ASP.NET pipeline. You could always have a nicer looking route that points to the same resource, but you are then going to pay for it one way or another. Why is it a problem to show the world the name of your theme? You can always rename that if necessary: "some-weird-theme-name-i-installed" is not a very good name :D

Jun 27, 2011 at 11:43 PM

right. yeah Im just looking at the normal user situation where they point and click to install a third party theme, which suddenly introduces a bunch of new/unknown/changed paths into their site from before. Not a biggie for me personally, I can always hack some url rewiting for my own purposes if I need to.

Jun 28, 2011 at 2:54 PM

I just remembered my reason for needing this- Im building a custom Layout.cshtml with my theme, and it makes a few references to images. So, this theme needs to know it's own name, and if the name changes later, it would any paths to the images. There's not way to use a relative path to theme images in this scenario... make sense?

Jun 28, 2011 at 4:29 PM

You can still write out the application base path in your absolute URLs; get it from Url.RequestContext.HttpContext.Request.ToRootUrlString(). That's the only bit that could change from one instance to another.

The module I mentioned by psy (that discussion I linked already) will work fine for your purposes here.

Jun 30, 2011 at 4:01 AM

@bertrand There's plenty of reasons why you wouldn't want to show the name of your theme or even the physical path for the images (no reason to get into them). It will always be faster to directly reference the static file or image, but if you're trying to be super efficient like that than you probably shouldn't use Orchard or any really CMS either. 


@boomhauer Check out the module I wrote, it's pretty fast, no performance tests done, but it feels fast (butt-dyno), and will let you reference your images however you want to. I mapped my themed images to something else as well. 

I wrote the module to just to remap images, and the URL's for any newly placed media are automatically updated (and some of the old ones), but the route path should work for any physical file. You could do a map like this /Themes/MyAwesomeTheme/Styles/ >>>>> /Styles/ and anything in your folder would be accessible by just using

The module could probably be extended easily to handle changing the Style.Include function to return the new mapped URL as well, but you can just easily change it in your layout file to say <link href="/Styles/Site.css" rel="stylesheet" type="text/css" /> as well and it will just work.


Jun 30, 2011 at 4:05 AM

Yeah that would be a cool addition, for it to detect the currently selected theme and allow the user to map a token or something like /{currenttheme}/content/images/... etc...