Copied to clipboard

Flag this post as spam?

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


  • Michael 24 posts 137 karma points
    Feb 08, 2014 @ 13:29
    Michael
    0

    Razor menu with protection and hidden doctypes

    Hi guys

    I am working on converting my super sweet XSLT files to razor. I kinda need at few tips/tricks or links now.

    I would like to add protection to my menu. In my xslt it's done like this:

    and (umbraco.library:IsProtected(@id, @path) = false() or umbraco.library:HasAccess(@id, @path) = true())

    The other thing I need to figure out is how to hide certian doctypes. In XSLT I do it like this:

    and name() != 'Produkt'

    My new razor menu looks like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var startNodeID = Parameter.source;
      var homeName = Parameter.frontPage;
      var homeLink = @Model.NodeById(startNodeID);
      var homeCurrent = "current";
    }
    @* Fjerner current class hvis ikke på forsiden (kigger på doctype alias) *@
    @if (Model.NodeTypeAlias != "Forside") {
      homeCurrent = "";
    }
    @if (startNodeID != null)
    {
      var startNode = Library.NodeById(startNodeID);
      if (startNode.Children.Where("Visible").Any())
      {
        <ul class="topmenu">
          <li class="@homeCurrent">
            <a href="@homeLink.Url">@homeName</a>
          </li>
          @* Loop undersider *@
          @foreach (var page in startNode.Children.Where("Visible"))
          {
            <li class="@page.IsAncestorOrSelf(Model, "current", "")">
              <a href="@page.Url">@page.Name</a>
              <ul>@* LVL 2 *@
                @foreach (var subpage in page.Children.Where("Visible"))
                {
                  <li><a href="@subpage.Url">@subpage.Name</a>
                          <ul>@* LVL 3 *@
                        @foreach (var subsubpage in subpage.Children.Where("Visible"))
                        {
                        <li><a href="@subsubpage.Url">@subsubpage.Name</a></li>
                        }
                      </ul>
                      </li>
                }
              </ul>
            </li>
          }
        </ul>
      }    
    }

    Any help/pointers would be much appreciated. :)

    /Michael

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 08, 2014 @ 14:12
    Dennis Aaen
    100

    Hi Michael,

    For the IsProtected or HasAccess http://umbraco.com/follow-us/blog-archive/2011/3/13/umbraco-razor-feature-walkthrough-part-5.aspx

    @foreach(var item in Model.Children)
    {
        if(item.HasAccess())
        {
            @item.Name
        }
    }

    The way that you can get nodes with at specific document type is:

    @foreach (var item in Model.NewsItems.Where("Visible"))

    Or you could do:

    @{
       
    // Get root node:
       
    var root =Model.AncestorOrSelf();

       
    // Get all descendants, filter by type:
       
    var nodes = root.Descendants("NewsItem");

       
    // Loop through the filtered nodes, displaying the properties:
       
    <ul>
       
    @foreach(var node in nodes)
       
    {
           
    <li>
               
    <h2>@node.infoTitle</h2>
                <div>@node.infoSummary</
    div>
               
    <div>@node.infoBody</div>
            </
    li>
       
    }
       
    </ul>
    }

    Hope that helps,

    /Dennis

  • Michael 24 posts 137 karma points
    Feb 08, 2014 @ 15:03
    Michael
    0

    Hi Dennis

    Thank you for the quick respond and the solution :)

    Upgraded my Razor so it now can hide both protected pages and the doctypes i need. My code now looks like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var startNodeID = Parameter.source;
      var homeName = Parameter.frontPage;
      var homeLink = @Model.NodeById(startNodeID);
      var homeCurrent = "current";
    }
    @* Fjerner current class hvis ikke på forsiden (kigger på doctype alias) *@
    @if (Model.NodeTypeAlias != "Forside") {
      homeCurrent = "";
    }
    @if (startNodeID != null)
    {
      var startNode = Library.NodeById(startNodeID);
      if (startNode.Children.Where("Visible").Any())
      {
        <ul class="topmenu">
          <li class="@homeCurrent">
            <a href="@homeLink.Url">@homeName</a>
          </li>
          @* Loop undersider *@
          @foreach (var page in startNode.Children.Where("Visible"))
          {
            if (page.HasAccess)
            {
            <li class="@page.IsAncestorOrSelf(Model, "current", "")">
              <a href="@page.Url">@page.Name</a>
              <ul>@* LVL 2 *@
                @foreach (var subpage in page.Children.Where("Visible"))
                {
                  if (subpage.HasAccess)
                  {
                  <li><a href="@subpage.Url">@subpage.Name</a>
                          <ul>@* LVL 3 *@
                        @foreach (var subsubpage in subpage.Children.Where("Visible"))
                        {
                        if (subsubpage.HasAccess && subsubpage.NodeTypeAlias != "doctypeAliasHere")
                        {
                        <li><a href="@subsubpage.Url">@subsubpage.Name</a></li>
                        }
                        }
                      </ul>
                      </li>
                  }
                }
              </ul>
            </li>
            }
          }
        </ul>
      }    
    }

    There might be a smarter way to build it, but it works now, thanks again Dennis :)

    /Michael

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies