Copied to clipboard

Flag this post as spam?

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


  • Mike 56 posts 88 karma points
    Jul 08, 2013 @ 21:49
    Mike
    1

    v6 razor docs?

    I feel like I'm flying blind here with absolutely no documentation on version 6...

    I'm assuming DynamicNode is no longer available in v6. If that's true, can someone at least update/add a new cheatsheat? http://our.umbraco.org/documentation/Cheatsheets/DynamicNodeRazor.pdf. 95% of the code samples I've seen all use DynamicNode, which don't seem to work as partial view macros (.cshtml) on version 6+

    I tried writing my own recursive navigation script, but realize I can't even write a recursive funciton since I don't even know what "nodes" are typecasted as without diving into the full source version. Does anyone have a sample as a starting point?

    Also it seems like in order to get anything done with a partial view macro I need to inherit Umbraco.Web.Mvc.UmbracoTemplatePage not the default Umbraco.Web.Macros.PartialViewMacroPage. Am I doing something wrong?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jul 08, 2013 @ 21:53
    Andy Butland
    1

    Here's what documentation there is currently.  In particular worth reading through the MVC section.

    Andy

  • Mike 56 posts 88 karma points
    Jul 09, 2013 @ 16:28
    Mike
    1

    Thanks for the links, but have read most of that (which also leads me to the outdated razor cheatsheet). Although the MVC section is somewhat helpful, it's exceedingly weak and I struggle to make significant progress even with prior MVC/Razor experience.

    For instance, this line for example straight out of the docs:

    @foreach(var child in Model.Content.Children())

    What type of variable is child? What are its properties and methods? Do I need to dive into the Umbraco source code to figure this out?

    Not to mention my original questions.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jul 09, 2013 @ 21:28
    Andy Butland
    2

    Children returns an IEmumerable<IPublishedContent> - but I can't see a class reference for this in the docs yet either.  I'd suggest the easiest way at this point would be to code your razor in VS.Net - as this is strongly typed, you'll get intellisense, which will reveal the properties and methods.

  • Funka! 398 posts 661 karma points
    Jul 10, 2013 @ 02:10
    Funka!
    1

    One of my frequent tricks is to just temporarily dump the type of the variable on the page so I can see it right there rather than ever trying to deduce from documentation or source code. For example, your question about what type of variable is "child"?

    <p>Type is @Model.Content.Children().GetType().ToString()</p>

    Output:

    Type is IEnumerable<IPublishedContent> (or whatever)

    So then if you are enumerating this you know what the individual child variable is. Or, if you code in Visual Studio, just hover over the var keyword and it will tell you there too. Eventually however you'll start to learn these and hopefully gets easier each time.

    Best of luck!

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 10, 2013 @ 10:17
  • Comment author was deleted

    Jul 10, 2013 @ 10:58
  • Mike 56 posts 88 karma points
    Jul 10, 2013 @ 17:53
    Mike
    0

    Thanks to everyone for their assistance, especially in identifying IPublishedContent (it is my understanding this has replaced DynamicNode in v4.10+). Here's my first stab at a v6 recursive razor menu. Some variables should probably be passed via the template, but I'm open to critiques.

     

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    var home = Model.Content.AncestorOrSelf(1);
    }

    <ul id="menu">
    @{ var active = ""; }

    @if(home.Id == CurrentPage.Id)
    {
    active = "active";
    }
    <li class="@active">
    <a href="@home.Url">@home.Name</a>
    </li>

    @*Loop through Home children*@
    @foreach (var node in home.Children.Where(x => x.IsVisible()))
    {
    @RecursiveMenu(node)
    }
    </ul>

    @helper RecursiveMenu(IPublishedContent node)
    {
    var active = "";
    var levelLimit = 3; // adjust as necessary

    if(Model.Content.AncestorOrSelf(1).Id != CurrentPage.Id)
    {
    @*See if we should show current page and ancestors as active*@
    if (node.Id == Model.Content.AncestorOrSelf(2).Id || node.Id == CurrentPage.Id)
    {
    active = "active";
    }
    }

    @*Returns true if current doctype is found in allowed list*@
    if(IsViewableDoctype(node.DocumentTypeAlias))
    {
    <li class="@active">
    <a href="@node.Url">@node.Name</a>
    @if(node.Children.Count() > 0 && node.Level < levelLimit)
    {
    <ul>
    @foreach (var subNode in node.Children.Where(x => x.IsVisible()))
    {
    @RecursiveMenu(subNode)
    }
    </ul>
    }
    </li>
    }
    }

    @functions{
    public bool IsViewableDoctype(string doctype)
    {
    // Filters to show only nodes of listed doctype aliases - adjust as necessary
    string[] validDoctypes = {"homepage", "landingPage", "contentPage"};

    if (validDoctypes.Contains(doctype))
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
  • Mike 56 posts 88 karma points
    Jul 10, 2013 @ 22:36
    Mike
    0

    There looks to be a small bug with this line:

     @*See if we should show current page and ancestors as active*@ 
    if (node.Id == Model.Content.AncestorOrSelf(2).Id || node.Id == CurrentPage.Id)
    {
    active = "active";
    }

    If I'm several levels deep, it will only show the current page and first level as active (not levels in between). Can someone point me in the right direction?

     

Please Sign in or register to post replies

Write your reply to:

Draft