Custom Menu - Wrap or override DisplayChildren()

Topics: Customizing Orchard, General, Writing modules
Aug 23, 2012 at 9:11 PM

Hi we are building a custom Menu.

I would like to take the standard nested output from the Orchard menu, and add a wrapper around it. For example consider the menu structure below:

<ul>
  <li><a href="#">Item1</a>
    <ul>
      <li class="featured"><a href="#">Item 1.1</a></li>
      <li><a href="#">Item 1.2</a></li>
      <li class="featured"><a href="">Item 1.3</a></li>
      <li><a href="#">Item 1.4</a></li>
    </ul>
  </li>
</ul>

I would like to capture this, load it into a XML document and modify it (or perhaps an XSLT transformation), my code will modify the default structure and looks for items called featured and should create the following output which I can then style:

<ul>
  <li><a href="#">Item1</a>
    <ul>
      <li>
        <ul>
          <li><a href="#">Item 1.1</a></li>
          <li><a href="#">Item 1.2</a></li>
          <li><a href="#">Item 1.3</a></li>
          <li><a href="#">Item 1.4</a></li>
        </ul>
      </li>
      <li>
        <ul class="featured">
           <li><a href="#">Item 1.1</a></li>
           <li><a href="#">Item 1.3</a></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

We are looking to the core file MenuItem.cshtml and create a custom theme - so far I have changed the below but before I go further need advice:

if (items.Any()) {
    <ul>
    @{
        var children = DisplayChildren(Model);
        var docXMLFile = new XmlDocument();
        docXMLFile.Load(children);
        // step 1 - loop through elements, test for attributes and insert list as required
        // step 2 - output this new list
    }
    </ul>
}

Being new to Orchard can you please let me know how I can perform the above, and get DisplayChildren to write a HTML string as opposed to outputting directly to the stream?

Is there a better way to achieve what I want above?

Is it ok to write this as a custom theme or would a plugin be better?

Many thanks,

Chris

Aug 24, 2012 at 4:09 AM

Why not just do this from the Navigation Admin?  You can nest menu items by dragging them to the right.  Nothing stopping you from adding the same menu item twice either.

Aug 24, 2012 at 6:38 AM
Hi because that is just one example of a number of customisations and we need to control how the menu is built so pointers where to go next are appreciated.



Sent from Samsung Galaxy Note

BrandonJoyce <notifications@codeplex.com> wrote:

From: BrandonJoyce

Why not just do this from the Navigation Admin? You can nest menu items by dragging them to the right. Nothing stopping you from adding the same menu item twice either.

Developer
Aug 25, 2012 at 4:20 AM

As far as I can tell, DisplayChildren does not write directly to the response stream. Instead it returns an IHtmlString:

public IHtmlString DisplayChildren(dynamic shape) {
            var writer = new HtmlStringWriter();
            foreach (var item in shape) {
                writer.Write(Display(item));
            }
            return writer;
        }