posts - 53, comments - 155, trackbacks - 4

My Links

Archives

Post Categories

Projects

Web Dev

Using a Recursive Expression to Create an Html Menu

I find recursive expression to be really useful. They are particularly handy when dealing with parent child relationships. As a demonstration, see a potential method for generating a site map below:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace SiteMapDemo

    7 {

    8     class MenuItem

    9     {

   10         public Guid ID { get; set; }

   11         public Guid? ParentID { get; set; }

   12         public string Name { get; set; }

   13         public string Path { get; set; }

   14         public int Rank { get; set; }

   15     }

   16     class Program

   17     {

   18         static void Main(string[] args)

   19         {

   20             List<MenuItem> menu = new List<MenuItem>(new[]{

   21                 new MenuItem{ID = Guid.NewGuid(), Name = "First", ParentID=null, Path="/", Rank=0},

   22                 new MenuItem{ID = Guid.NewGuid(), Name = "Second", ParentID=null, Path="/second.aspx",Rank=1},

   23             });

   24             menu.AddRange(new[] {

   25                 new MenuItem{ID = Guid.NewGuid(), Name = "FirstSub", ParentID=menu[0].ID, Path="/firstsub.aspx",Rank=0},

   26                 new MenuItem{ID = Guid.NewGuid(), Name = "SecondSub", ParentID=menu[0].ID, Path="/secondsub.aspx",Rank=1},

   27                 });

   28             Func<List<MenuItem>, Guid?, string> renderMenu = null;

   29             renderMenu = (menus, Parent) =>

   30             {

   31                 var sub = menus.Where(m => m.ParentID == Parent).OrderBy(s => s.Rank).ToList();

   32                 if (sub.Count > 0)

   33                 {

   34                     StringBuilder sb = new StringBuilder();

   35                     sub.ForEach(s => { sb.Append(String.Format("<li><a href=\"{0}\">{1}</a>{2}</li>", s.Path, s.Name, renderMenu(menus, s.ID))); });

   36                     return sb.ToString();

   37                 }

   38                 return "";

   39             };

   40             Console.Write(renderMenu(menu, null));

   41             Console.ReadLine();

   42         }

   43     }

   44 }

The output:

<li><a href="http://www.charlesrcook.com/">First</a></li><li><a href="http://www.charlesrcook.com/firstsub.aspx">FirstSub</a></li><li><a href="/secondsub.aspx">SecondSub</a></li><li><a href="http://www.charlesrcook.com/second.aspx">Second</a></li>

Print | posted on Thursday, January 08, 2009 7:40 PM | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Feedback

Gravatar

# re: Using a Recursive Expression to Create an Html Menu

Good one here! There was a time when I wondered what people meant by recursive expressions, in my initial programming days when I didn’t know a code from a code! And HTML was an acronym they showed near gmail attachments! Do you know the best explanation I got when I asked about recursive expressions? It spoke of ‘regular expressions’ in our day-to-day life which are not really ‘regular’ in any way. It said that a regular expression has “itself in a ‘code’ part”! And this subsequently becomes a recursive regular expression! This is not without issues, but still, it is really useful!
5/12/2010 2:50 AM | Coursework

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 4 and type the answer here:

Powered by: