Sunday, January 09, 2005
DasBlog : Adding page templates for individual pages

As described here, here are the quick and dirty details on how I implemented multiple templates in DasBlog. 

Each page in the DasBlog system inherits from SharedBasePage, which has a virtual method GetPageTemplate.  All of the pages I investigated defered to the base SharedBasePage.GetPageTemplate, which in turn calls GetHomeTemplate. 

Being as all these methods are there, I either assume there are parts of the framework that actually already dynamicly load templates, or there were plans to add these features in the future.  This being the case, I wanted my implementation to touch as few places as possible so I can yank it out if I need to. 

To quickly implement this, I added some code to the GetHomeTemplate method in the SharedBasePage that will :
1) Get Current Page Name (ie Permalink.aspx)
2) Attempt to load a template for that page (ir permalink.blogtemplate)
3) return the custom page, or if null return the home template

To accomadate this, I also added an OpenNamedTemplate passthrough method in the themes.cs file.  Once again, these two minor changes now facilitate each page having their own template by simply adding a new template permalink.blogtemplate.

You may ask, why would you want this?  The main reason is to try to emulate how MT works when someone clicks an individual link.  So once I had this, I added a new permalink.blogtemplate, and removed the DIV's that handle the right sidebar content.  Easy as cake.  I haven't found another place to use it, but I'm sure I will as I continue to dive into DasBlog.

Complete Change :

In SharedBasePage.cs:newtelligence.DasBlog.Web.Core.SharedBasePage (CHANGE)

  /* [DynamicPageTemplates]
   * Changed to allow dynamic templates.  Will parse out the page name, and attempt to
   * load a template based on that name.  If it finds one, it will use it, otherwise
   * will drop back to home template. ajj 1.7.04 [/DynamicPageTemplates]
   */
  public virtual string GetHomeTemplate(string path)
  {
   string templateString="";
   string localPath = this.Request.Url.LocalPath;
   string pageName = localPath.Substring(localPath.LastIndexOf(@"/") + 1 ,localPath.Length - (localPath.LastIndexOf(@"/") + 1)).Replace(".aspx",string.Empty);
   using ( TextReader sr = Theme.OpenNamedTemplate(path,CategoryName,pageName) )
   {
    if (sr != null)
    {
     templateString = sr.ReadToEnd();

    }
   }
   if (templateString == string.Empty)
   {
    using ( TextReader sr = Theme.OpenHomeTemplate(path, CategoryName ) )
    {
     templateString = sr.ReadToEnd();
    }
   }
   return templateString;
  }

In Themes.cs:newtelligence.DasBlog.Web.Core.Theme (NEW CODE)

  /* [DynamicPageTemplates]
   * Added this method to load a named template to allow a page to load
   * any template ajj 1/7/05 [/DynamicPageTemplates]
   */
  public TextReader OpenNamedTemplate(string basePath, string categoryName, string templateName)
  {
   TextReader templateReader = OpenTemplate(templateName,basePath,categoryName);
   if (templateReader == null)
   {
    return null;
   }
   return templateReader;
  }