Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chuck 71 posts 69 karma points
    Jul 12, 2012 @ 18:43
    Chuck
    0

    Doctype and Traverse

    So I am working on a tansverse razor script and I want to be able to pass the name of the doctype I want to list out into the helper and then in my foreach loop use a variable that contains the name of the doctype. Is there anyway to do this. This is what i have and it's not working. 

    @traverse("ProductCategory",parent,startNode)
    @helper traverse(string docTypeNamedynamic parent{
          <ul>                                                       
          @foreach (var node in parent.ProductCategory{
          <li>
            <href="@node.Url">@node.Name</a>
            @if (@node.Children.Count(0{@traverse(docTypeName,node);}
          </li>
          }
          </ul>  
    }

     

     

  • Chuck 71 posts 69 karma points
    Jul 12, 2012 @ 18:45
    Chuck
    0

    actually thats wrong... my foreach loop looks like this. 

     

    @helper traverse(string docTypeName, dynamic parent) {
          <ul>                                                       
     
          @foreach (var node in parent.docTypeName) {
          <li>
            <href="@node.Url">@node.Name</a>
            @if (@node.Children.Count(0{@traverse(docTypeName,node);}
          </li>
          }
          </ul>  
    }

      

  • Douglas Ludlow 210 posts 366 karma points
    Jul 12, 2012 @ 18:57
    Douglas Ludlow
    0

    This is what you'll want to do:

    @helper traverse(string docTypeName, dynamic parent)
    {
    if (parent.Children.Count() > 0)
    {
    <ul>
    @foreach (var node in parent.Children.Where("NodeTypeAlias == @0", docTypeName))
    {
    <li>
    <a href="@node.Url">@node.Name</a>
    @traverse(docTypeName, node)
    </li>
    }
    </ul>
    }
    }
  • Chuck 71 posts 69 karma points
    Jul 12, 2012 @ 19:02
    Chuck
    0

    Doug, thanks, the trick for me was in how you targeted the NodeTypeAlias... I have never seen the ==@0... what does that mean? How does that work? 

  • Douglas Ludlow 210 posts 366 karma points
    Jul 12, 2012 @ 19:20
    Douglas Ludlow
    0

    When dealing with dynamic objects you can't use the regular .Where linq method that you'd normally use in a standard .NET application. So the Umbraco developers added support by creating their own Where method. This method uses a string replace simular to .NET's String.Format method to add string variables in as parameters of the method, only String.Format uses '{0}' instead of '@0'. The number after the "at" sign is the index of the parameter. So if you were writing a Where statement with more than one parameter, you could do something like the following:

    var nodes = Model.Children.Where("NodeTypeAlias == @0 && someOtherProperty == @1","alias","someValue");

    Hope that helps you understand it some more.

Please Sign in or register to post replies

Write your reply to:

Draft