Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bex 444 posts 555 karma points
    Jul 13, 2010 @ 18:07
    Bex
    1

    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

  • wolulcmit 357 posts 693 karma points
    Jul 13, 2010 @ 23:59
    wolulcmit
    2

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

     

  • Bex 444 posts 555 karma points
    Jul 14, 2010 @ 09:51
    Bex
    0

    This may work. just changing the style sheet, but there must be a way of changing the actual theme?

  • wolulcmit 357 posts 693 karma points
    Jul 14, 2010 @ 10:13
    wolulcmit
    0

    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

  • Bex 444 posts 555 karma points
    Jul 14, 2010 @ 10:17
    Bex
    0

    I think I have just found a way... am just testing and if it works I will post it! :)

  • Bex 444 posts 555 karma points
    Jul 14, 2010 @ 10:34
    Bex
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft