I have a few folders in my umbraco, with each one having a different domain assigned to it, each domain has a theme assigned to it, which is stored in a database.
So when a page loads I need to be able to set the theme in the code by doing page.theme=...
Having done this previously outside umbraco I have just set the OnPreInit of a base page that all pages inherit from, but how on earth do I do this in umbraco?
Do I need to edit the core code? or is there some other way of doing this that is already implemented?
why not just have an extra property on each of your top-level datatypes...call it something like 'theme' and then use this value to change your stylesheets.
A basic example I did was this: and then in my templates I used this value to include a different stylesheet which would overide the sites colours and use the appropriate animal for each case: <linkrel="stylesheet"type="text/css"href="/css/value-from-umbraco-item-here.css"/>
no need for writing any code on my part.... hopefully you should be able to achieve the same even if it's slightly more complicated in your case, and you shouldn't have to do any fancy schmancy c# stuff.
Setting Page theme Via Code
Hello!
I have a few folders in my umbraco, with each one having a different domain assigned to it, each domain has a theme assigned to it, which is stored in a database.
So when a page loads I need to be able to set the theme in the code by doing page.theme=...
Having done this previously outside umbraco I have just set the OnPreInit of a base page that all pages inherit from, but how on earth do I do this in umbraco?
Do I need to edit the core code? or is there some other way of doing this that is already implemented?
Thanks
Bex
why not just have an extra property on each of your top-level datatypes...call it something like 'theme' and then use this value to change your stylesheets.
![](/media/upload/63845999-b011-4471-ada7-5f517bc6c90e/animal.jpg)
A basic example I did was this:
and then in my templates I used this value to include a different stylesheet which would overide the sites colours and use the appropriate animal for each case:
<link rel="stylesheet" type="text/css" href="/css/value-from-umbraco-item-here.css" />
no need for writing any code on my part.... hopefully you should be able to achieve the same even if it's slightly more complicated in your case, and you shouldn't have to do any fancy schmancy c# stuff.
This may work. just changing the style sheet, but there must be a way of changing the actual theme?
There probably is a way to do that, maybe someone else can step in and help?
what do asp.net theme's do that css and umbraco templates can't?
you could also use a different template for each top level node/domain, which would give you a lot of flexibility....
- Tim
I think I have just found a way... am just testing and if it works I will post it! :)
Ok, here's how I did it:
I found this here:
http://bloggingabout.net/blogs/rick/archive/2007/10/20/howto-set-the-theme-for-a-masterpage-from-code.aspx
public class ThemeModule:IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += delegate
{
string theme=DetermineTheme();
if (theme !=string.Empty)
{
page.Theme = theme;
}
};
}
}
#endregion
string DetermineTheme()
{
string host = HttpContext.Current.Request.Url.Host;
//here I'm checking the database for the theme against the url
}
}
Add the following to your web config:
<httpModules>
<add name="ThemeModule" type="namespacename.ThemeModule"/>
</httpModules>
Then just compile it as normal and add the dll to your umbraco project!
Hope this helps someone else!
Bex
is working on a reply...