Clean, Horizontal dasBlog Navigation

by Administrator 11. October 2007 17:03

I recently upgraded to dasBlog 2.0 and went ahead and updated my site design while I was at it.  With the new design, I wanted to use dasBlog's navigator links for the horizontal menu.  However, the navigatorLinks macro is hard-coded with a vertically-oriented table structure.  So I wrote a custom macro to provide the links in a simple unordered list that I could style with CSS.  I started from the source code for the navigatorLinks macro and just replaced the HTML-generating bits.  While I was tempted to consolidate the redundant logic, I avoided any other refactoring to save time.  For those interested, my code is below.  For more information on creating custom macros, read: Creating custom macros for dasBlog.

public class CustomMacro

{

    protected SharedBasePage requestPage;

    protected Entry currentItem;

 

    public CustomMacro(SharedBasePage page, Entry item)

    {

        requestPage = page;

        currentItem = item;

    }

 

    /// <summary>

    /// Return the navigator links in an unordered list.

    /// </summary>

    public Control GetNavigatorList()

    {

        string fileName = "navigatorLinks.xml";

        StringBuilder navigator = new StringBuilder("<div class=\"navigatorContainer\"><ul class=\"navigatorList\">");

        string itemTemplate = "<li class=\"navigatorListItem\"><a class=\"navigatorListItemLink\" href=\"{0}\">{1}</a></li>";

 

        try

        {

            string fullPath = HttpContext.Current.Server.MapPath(SiteConfig.GetSiteConfig().ContentDir + fileName);

            if (File.Exists(fullPath))

            {

                NavigatorXml nav;

                using (Stream s = File.OpenRead(fullPath))

                {

                    XmlSerializer ser = new XmlSerializer(typeof(NavigatorXml));

                    nav = (NavigatorXml)ser.Deserialize(s);

                }

 

                foreach (NavigatorItem navitem in nav.Items)

                    navigator.Append(String.Format(itemTemplate, navitem.Url, navitem.Name));

            }

            else

            {

                NavigationRoot nav;

                fullPath = HttpContext.Current.Server.MapPath("~/SiteConfig/" + fileName);

                if (File.Exists(fullPath))

                {

                    using (Stream s = File.OpenRead(fullPath))

                    {

                        XmlSerializer ser = new XmlSerializer(typeof(NavigationRoot));

                        nav = (NavigationRoot)ser.Deserialize(s);

                    }

 

                    foreach (NavigationLink navitem in nav.Items)

                        navigator.Append(String.Format(itemTemplate, navitem.Url, navitem.Name));

                }

                else

                {

                    return new LiteralControl("Add '" + fileName + "' to your SiteConfig directory<br />");

                }

            }

        }

        catch (Exception exc)

        {

            ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error, exc);

            return new LiteralControl("There was an error processing '" + fileName + "'<br />");

        }

 

        navigator.Append("</ul></div>");

 

        return new LiteralControl(navigator.ToString());

    }

}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Design / Technique | Tools / Services

Comments are closed

About Me

I'm an Internet technology business strategist, software architect, and development leader specializing in interactive marketing and social media.  read more...