Two themes, one blog

Topics: Themes
Jun 20, 2012 at 4:10 PM
Edited Jun 20, 2012 at 7:25 PM

I have integrated BlogEngine into my existing asp.net website. SQL is on. Everything works great! My thanks to all of the contributors!

I have created a custom theme, and that also works perfectly. I want to add a second one.

I want to use two different master pages, depending on where the user enters the blog. I know I can use "?theme=othertheme" but how do I get that theme to persist as long as I want?  When I click on a post, it reverts to the default theme?

I would really like to have two separate landing pages, each with it's own master page. Any ideas on how to accomplish this?

Something like http://www.foo.com/theme1 and http://www.foo.com/theme2 both sharing exactly the same database and posts.

I am open to any suggestions which will get me to the same goal>

Coordinator
Jun 20, 2012 at 8:07 PM

If you look at http://dnbegallery.org/preview site, it does pretty much what you want. For example try:

http://dnbegallery.org/preview/default.aspx?theme=notepad-chaos

You'll see that it will use notepad theme as current. But if you try same site with boldy:

http://dnbegallery.org/preview/default.aspx?theme=boldy

it will use boldy instead.

Basically on default page load if query sting has theme in it, it is set as a cookie. Then method in the core that returns current theme modified to check if there is a cookie for a current theme and if it is, it returns this as a current selected theme.

Jun 21, 2012 at 1:16 AM

Thanks for the reply.

However, entering the theme in the query string does not seem to work. I have created a custom theme, and set it as default.

http://foo.com/blog gives me default custom theme

If I enter http://foo.com/blog?theme=standard it gives me the same page, with a different theme. Great....

However, when I click on a post, even after using the query string, I get the default theme again.

How can  I modify this behavior to reliably give me the correct results?  I don't want to append every url in code. I think that may cause problems down the road. 

Jun 22, 2012 at 6:45 PM

Am I being unclear in my explanation?

Am I doing something wrong?

I cannot get this to work as explained above

Is there an alternate way to achieve the desired results?

Coordinator
Jun 22, 2012 at 7:16 PM

I guess, its me been unclear in explanation :) It doesn't do it "out of the box" - you need to modify code to first set a cookie and then look for that cookie to identify which theme to use.

Jun 22, 2012 at 7:42 PM

Please, can you show me an example of how to do this?

I am able to access the theme through: BlogSettings.Instance.Theme

But how do I set it?

Jun 22, 2012 at 7:46 PM

If I can set the theme directly, I can work around this problem.

Or, if I can set the cookie, that would work also. Although I do not know how to do that

Please show me an example.

Coordinator
Jun 22, 2012 at 9:22 PM

Here's an example from an older version of BE you can refer to:
http://dbvt.com/blog/post/Supporting-Multiple-Master-Pages-in-BlogEngineNET.aspx

The master page is being dynamically set in OnPreInit in BlogBasePage.cs in the BlogEngine.Core.

There, you can determine which theme you want to display and set it. 

Jun 27, 2012 at 11:46 PM
Edited Jun 27, 2012 at 11:49 PM

I tried to implement that solution, but it did not work. Can someone please show me an example that will work on BE 2.6?

Or, maybe I am doing something wrong? Maybe a little more complete explanation of what is being done, and to which files. 

 

Thanks. I am a bit overloaded at the moment, and any extra help is appreciated. 

Jun 28, 2012 at 6:39 PM

Tell me if I did something wrong:

1. I added to the BlogBasePage.cs the following code (in red):

public abstract class BlogBasePage : Page
    {
        #region Public Methods

    

        private string xMasterPage = "site.master";
        private string _Theme;
    
        public string XMasterPage
        {
        get
            {
            return xMasterPage;
            }
        set
            {
            xMasterPage = value;
            }
        }
         

 

........

also added to BlogBasePage.cs


        protected override void OnPreInit(EventArgs e)
        {
            bool allowViewing = false;



            if (Request.QueryString["theme"] != null) _Theme = Request.QueryString["theme"];
            
            MasterPageFile = Utils.RelativeWebRoot + "themes/" + _Theme + "/" + this.XMasterPage;

I created a new master page called test.master in the current theme directory. 

Then, I created a copy of the Default.aspx , named it default_new.aspx..... Renamed a few things, and added the following to the default_new.aspx.cs:

 

   protected override void OnPreInit(EventArgs e)
        {
        this.DAHMasterPage = "test.master";
        base.OnPreInit(e);
        }

It compiles, but does not work. I am still using the same master page. What am I missing? 

 

Jun 28, 2012 at 8:33 PM
Edited Jun 28, 2012 at 8:39 PM

I have also tried the following from the post:

http://www.joshuaterry.com/post/2011/10/27/Quick-Tips-Multiple-Master-Pages-for-BlogEnginenet.aspx

I put the in the code behind for my new "landing" page. This seems to work... However it gives me the same problem as before with persistence. The theme does not persist. Whenever I click on a link, it goes back to the old theme. 

protected override void OnPreInit(EventArgs e)
    {  
        base.OnPreInit(e);
        this.MasterPageFile = string.Format("{0}themes/{1}/pages.master", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(null));
    }

Yes, it uses a new master page... great! 

The only problem is that when I click on any link on the page, I get the old master page again. 

Jun 28, 2012 at 8:45 PM

I know this can be done. I just don't know how to do this easily. Please help me!

Requirements:

  • I want to have page1.aspx use theme1 (or master page1)
  • I want to have page2.aspx use theme2 (or master page2)
  • I want to be able to click anywhere on page1 and have it continue to use theme1 (or master page1)
  • I want to be able to click anywhere on page2 and have it continue to use theme2 (or master page2)
Coordinator
Jun 29, 2012 at 3:43 AM

The key here is GetThemeWithAdjustments method. You can modify it to return value that you need to save in the cookie, something like:

If(Request.Cookies["the-custom-theme-cookie"] != null){
  return Request.Cookies["the-custom-theme-cookie"].Value;
}

Then in page1.aspx you set this cookie to theme1, and in page2.aspx to theme2. Whenever user gets to page one, cookie is set and theme1 used until user hits page2, then again cookie reset to theme2 and so on.

Jun 29, 2012 at 6:34 PM

I appreciate the help, but I still cannot implement this modification. I am not familiar/ confident enough with this code to make the modifications without a little more help. 

For example, I tried to add that line of code to the GetThemeWithAdjustments method (in BlogSettings.cs) and it will not compile. 

Jul 2, 2012 at 6:00 PM

I am still having problems figuring out the solution to this problem. Someone please help me!!!!! 

I do not understand well enough how the existing BE code works. I also have some holes in my knowledge about c# and the.net platform.

I would prefer to solve the problem myself, but I need to know what I should be focused on.  This is a fairly large project, and I don't really know how a lot of it works.  

One reply to my post states that I should be looking at GetThemeWithAdjustments but I don't know how that method works. I tried to implement the cookie idea, but I could not get it to compile. It is very difficult when I do not have confidence in this code, nor the advice I am getting.  

Another reply states that I should be modifying the base page. 

Can someone please give me an explanation of what I should be focused on, how it works, and maybe some ideas of how to modify it, I would be eternally grateful. Or, maybe you know of a better resource than this forum to solve my problem. Please share that also. I am kind of lost at the moment.I would be willing to take any advice at the moment. 

Coordinator
Jul 2, 2012 at 7:51 PM

Add this code to the end of Page_Load method in default.aspx.cs page (default page in the blog root, not in the theme):

var cn = BlogSettings.Instance.ThemeCookieName;
var uri = Request.Url.AbsoluteUri;

if(Request.QueryString[cn] != null)
{
  Response.Cookies.Add(new HttpCookie(cn, Request.QueryString[cn]) { Expires = DateTime.Now.AddYears(1) });
  Response.Redirect(uri.Substring(0, uri.IndexOf(cn + "=")));
}

Now if you navigate to your blog with "mysite.com/blog/default.aspx?theme=indigo" it should load "indigo" theme and theme should stick.

It should do it because this code looks at query string and if it has theme parameter it will set cookie with name = "theme" and value "indigo" in this case.

The "ThemeCookieName" is a setting that can be changed in the admin panel, so you use it instead of hard-coding "theme".

Second line in the "if" just to reload the page applying new theme.

If it doesn't compile, it is because I don't have VS around and just typing it, so there could be silly typo in the code.

Hope this helps.

Jul 2, 2012 at 9:17 PM

YES!!!!

Thank you so much!

This is exactly what I want!