Copied to clipboard

Flag this post as spam?

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


  • Nicky Christensen 76 posts 166 karma points
    Sep 24, 2012 @ 17:20
    Nicky Christensen
    0

    AncestorOfSelf in navigation?

    Hey Guys...

    Im having problems getting my HomePage (topnode) shown in my navigation...

    My sitetree looks like

    - Home
      - Blog
      - About
      - Contact

    However, my navigation only ouputs: Blog, About and Contact, It should Output "Home" aswell? 

    @RAZOR *@
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @
      var startLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.StartLevel);
      var finishLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.FinishLevel);   
      var parent @Model.AncestorOrSelf(startLevel);
      if (parent != null@traverse(parent,startLevel,finishLevel;  }
    }                                                     
                                                    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
     
     <ul>
          @foreach (var node in parent.Children.Where("Visible"))
        {
          var selected Array.IndexOf(Model.Path.Split(',')node.Id.ToString()>= " class=\"selected\"" "";
          <li@Html.Raw(selected)>
            <href="@node.Url">@node.Name</a>                                       
            @if (selected!=""&&@node.Level<=finishLevel@traverse(node,startLevel,finishLevel)}  
          </li>
          }
        </ul>   
    }

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 24, 2012 @ 19:11
    Fuji Kusaka
    0

    Hi Nicky ,

    If you want your top node "Homepage" to display as part of your navigation you could try something like

        const string selectedClass = " class=\"selected\"";
        var homeNode = Home;
     
      <[email protected](Model.NodeTypeAlias == "HOME",  selectedClass)><a href="/">@homeNode</a></li>

    Before your foreach loop

    //fuji

     

  • Nicky Christensen 76 posts 166 karma points
    Sep 24, 2012 @ 20:48
    Nicky Christensen
    0

    Hmm, then I just get an error? 

    Error occured

    d:\web\localuser\mydomain\public_html\macroScripts\634841164413937208_FitnessMainMenu.cshtml(18): error CS0103: The name 'selectedClass' does not exist in the current context

     

    The whole code is:

    @RAZOR *@
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @
      var startLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.StartLevel);
      var finishLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.FinishLevel);   
      var parent @Model.AncestorOrSelf(startLevel);
      if (parent != null@traverse(parent,startLevel,finishLevel;  }
    }                                                     
                                                    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
     
     <ul>

       const string selectedClass " class=\"selected\"";
        var homeNode Home;
      
      <li@Library.If(Model.NodeTypeAlias == "FitnessFrontPage",  selectedClass)><href="/">@homeNode</a></li>
     
        @foreach (var node in parent.Children.Where("Visible"))
        {
          var selected Array.IndexOf(Model.Path.Split(',')node.Id.ToString()>= " class=\"selected\"" "";
          <li@Html.Raw(selected)>
            <href="@node.Url">@node.Name</a>                                       
            @if (selected!=""&&@node.Level<=finishLevel@traverse(node,startLevel,finishLevel)}  
          </li>
          }
        </ul>   
    }

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 24, 2012 @ 20:52
    Fuji Kusaka
    0

    Here

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
      var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel);  
      var parent = @Model.AncestorOrSelf(startLevel);
      if (parent != null) { @traverse(parent,startLevel,finishLevel) ;  }
      const string selectedClass = " class=\"selected\"";
        var homeNode = Home;

     

  • Nicky Christensen 76 posts 166 karma points
    Sep 24, 2012 @ 20:59
    Nicky Christensen
    0

    Still getting the same error, that it doesnt exist in the current context:

    Error occured

    d:\web\localuser\penge-portal.dk\public_html\macroScripts\634841170896372593_FitnessMainMenu.cshtml(17): error CS0103: The name 'selectedClass' does not exist in the current context

     

    @RAZOR *@
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @
      var startLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.StartLevel);
      var finishLevel String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.FinishLevel);   
      var parent @Model.AncestorOrSelf(startLevel);
      if (parent != null@traverse(parent,startLevel,finishLevel;  }
          const string selectedClass " class=\"selected\"";
          var homeNode Home;
    }                                                     
                                                    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
     
     <ul>
      
      <li@Library.If(Model.NodeTypeAlias == "FitnessFrontpage",  selectedClass)><href="/">@homeNode</a></li>
     
        @foreach (var node in parent.Children.Where("Visible"))
        {
          var selected Array.IndexOf(Model.Path.Split(',')node.Id.ToString()>= " class=\"selected\"" "";
          <li@Html.Raw(selected)>
            <href="@node.Url">@node.Name</a>                                       
            @if (selected!=""&&@node.Level<=finishLevel@traverse(node,startLevel,finishLevel)}  
          </li>
          }
        </ul>   
    }

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 24, 2012 @ 21:13
    Fuji Kusaka
    0

    Nicky,

    Very sorry my bad.....was doing something and didnt notice i added some silly errors in this threat. I was actually using a dictionary item to the var homeNode.

    So instead it should be this

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
      var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel);  
      var parent = @Model.AncestorOrSelf(startLevel);
      if (parent != null) { @traverse(parent,startLevel,finishLevel) ;  }
              
    }                                                    
                                                   
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
        const string selectedClass = " class=\"selected\"";
     
     <ul>
     
      <[email protected](Model.NodeTypeAlias == "FitnessFrontpage",  selectedClass)><a href="/">Home</a></li>
     
        @foreach (var node in parent.Children.Where("Visible"))
        {
          var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          <[email protected](selected)>
            <a href="@node.Url">@node.Name</a>                                      
            @if (selected!=""&&@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); } 
          </li>
          }
        </ul>  
    }
  • Nicky Christensen 76 posts 166 karma points
    Sep 24, 2012 @ 21:23
    Nicky Christensen
    0

    Ahhh great, thx alot mate, works perfectly :) 

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 08, 2013 @ 23:29
    Craig100
    0

    This looked promising but in Umb 6.02 this gives: Encountered end tag "ul" with no matching start tag. Are your start/end tags properly balanced? 

    What's the best way to fix it?

    Craig

     

  • gary 385 posts 916 karma points
    Mar 09, 2013 @ 00:11
    gary
    0

    Hi Craig

    <li@library and <li@Html look a bit suspect, but Umbraco 6 is strict Razor 2, and if you have an @ where it shouldn't be it breaks code, so if it isn't your li's it might be the &&@node.Level , you could try removing the @ and see if it runs.

     

     

     

     

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 09, 2013 @ 14:32
    Craig100
    0

    No, can't get it to save at all in the Umbraco UI. And in VS2012 can't get Razor syntax checking. It insists on doing html5 checking on the cshtml file! Ahhh! Very frustrating. I just want a normal navigation macro that starts at the home page rather than one level down, which is what the pre-built example does for some odd reason. You'd think the pre-built example would cover the whole thing rather than leave you without a home page!

  • gary 385 posts 916 karma points
    Mar 09, 2013 @ 14:41
    gary
    0

    Hi 

    Not completely sure I understand, but if you block out the code section by section you will find where your error is, ie get it to save and then work on the part that causes it not to save.

    You have a level parameter for startLevel which is currently set to 1, so any children of that will be level 2, changing this parameter to 0, should give you the homepage if you have the "regular" Umbraco tree set-up.

    If I can help some more, please let me know.

    G

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 09, 2013 @ 16:07
    Craig100
    0

    Hi,

    It's not that it won't run, it won't even save in the Umbraco UI.

    Even if I strip it back to the following where the ul tags are clearly "in balance"

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
      var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 0 : int.Parse(Parameter.StartLevel);
      var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.FinishLevel);   
      var parent = @Model.AncestorOrSelf(startLevel);
      if (parent != null) { 
          @traverse(parent,startLevel,finishLevel);
      }           
    }                                                     
    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
        const string selectedClass = " class=\"menuLeftDivActive\""; 
        <ul> 
    
        </ul>
    }

    The error on saving is "c:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\9c9cf0f0\1b14c0a9\App_Web_634984379898353587_mainmenu.cshtml.70df5e80.5pgeb38h.0.cs(91): error CS1513: } expected"

    Doesn't look like it needs an extra "}" anywhere to me.  Don't know if there's something about Razor I'm just not getting or if there's a real issue. Wasting a lot of time with Razor syntax lately.

    Craig

  • gary 385 posts 916 karma points
    Mar 09, 2013 @ 16:59
    gary
    0

    Hi Craig

    Have had similar issues myself, but this is not Umbraco, its the mvc bit with razor syntax. There is a post on it somewhere, but I cant find it at the moment.

    var parent = Model.AncestorOrSelf - shouldn't need the @, this used to work, but takes it out of code, so hence you have no } to close.

    @{ 
     
    var startLevel =String.IsNullOrEmpty(Parameter.Level)?0:int.Parse(Parameter.StartLevel);
     
    var finishLevel =String.IsNullOrEmpty(Parameter.Level)?1:int.Parse(Parameter.FinishLevel);  
     
    var parent =Model.AncestorOrSelf(startLevel);
    }

      @
    if(parent !=null){
             
    traverse(parent,startLevel,finishLevel);
     
    {                                                 
                                                   
    helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
       
    conststring selectedClass =" class=\"menuLeftDivActive\"";
       
    <ul>

       
    </ul>
    }
    }
    }  

    Without the whole code, thats the best I can see, it appears your if statement goes nowhere, ie its closed too early. I have taken to calling the node vars in a separate code block at the top, then starting a new codeblock for any other statement. 
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 09, 2013 @ 17:00
    Dirk De Grave
    0

    I don't think you need the '@' sign before traverse in your if statement, as you're already in c# mode

     

    Cheers,

    /Dirk

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 09, 2013 @ 17:28
    Craig100
    0

    Thanks, got it to save now. But it ouputs nothing at all. All I have is a home page and an about page:-

    Home
    ----About 

    So set startLevel at 0 and finishLevel at 1.

    Another oddity is you can't add a variable inside an html tag properly without leaving an unnecessary space. i.e. <li @isHomeSelected> if isHomeSelected is nothing will give <li >. If there is no space, as you would want, to give <li>..... <li@isHomeSelected> then it won't save.

    The final code I have is:-

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 0 : int.Parse(Parameter.StartLevel);
     var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.FinishLevel);  
      var parent = Model.AncestorOrSelf(startLevel);
     if (parent != null) {traverse(parent,startLevel,finishLevel);}          
    }  
                                                                                                   
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
    var selectedClass = " class=\"menuLeftDivActive\"";
    <ul>
    @{var isHomeSelected = Library.If(Model.NodeTypeAlias == "Home",  selectedClass);}
     <li @isHomeSelected><a href="/">Home</a></li> 
        @foreach (var node in parent.Children.Where("includeInMainNav == true"))
         var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? selectedClass : "";
         <li @selected>
           <a href="@node.Url">@node.Name</a>                                      
            @if (selected != "" && node.Level <= finishLevel) {traverse(node,startLevel,finishLevel);}  
         </li>
         }
     </ul>  
    }
    If there's anything obvious as to why it won't work it would be great to hear it. Razor is getting to be unusable for me as it's taking too long to get stuff done.
    Craig

     

  • gary 385 posts 916 karma points
    Mar 09, 2013 @ 18:27
    gary
    0

    Hi Craig

    Glad it saves at least. The space is a new "rule", but I couldn't bring it to mind why the <li@ looked supspect, but you have it done now.

    Ok, if you have nothing above Home, it should only output, About, it is important to have a root node if you need to use the tree in this way. This blog post from Sebaastian is still relevant. http://cultiv.nl/blog/2010/12/19/tip-of-the-week-the-ultimate-site-structure-setup/

    Because you call parent.children, it will only output About.

    .Where("includeInMainNav == true") if this is a yes/no, I belive it outputs a zero or one, so won't return for true. The usual way is to have a yes/no property on the docType with an alias of umbracoNaviHide, then you can use the .Where("Visible").

    Still trying to find a documant on razor V2, sure it would clear up some of these niggles. But XSLT - -  no chance.

    G

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 09, 2013 @ 19:01
    Craig100
    0

    Thanks Gary,

    I gave up with this in the end and re-engineered it from a few other posts and documents.

    includeInMainNav is a true/false field in the docType. In this site there are more pages hidden than not as a lot of them are page parts, so this logic was more expedient.

    This now compiles and works nicely. Hope it helps someone else:-

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <ul>
       @{
            var homeNode = Model.AncestorOrSelf("Home");          //Sets the start point     
            var homeActive = Library.If(Model.NodeTypeAlias == "Home", "menuLeftDivActive", "");
           <li class="@homeActive"><a href="/" title="Home">Home</a></li>
           foreach (var page in homeNode.Children.Where("includeInMainNav == true"))
           {
               var pageActive = "";
               if (Array.IndexOf(Model.Path.Split(','), page.Id.ToString()) >= 0) {
    pageActive = "menuLeftDivActive";
          }
    pageActive += page.IsLast(" menuLeftDivLast","");
    <li class="@pageActive.Trim()"><a href="@page.Url" title="@page.name">@page.name</a></li>
           }
       }
    </ul>

    Craig

  • gary 385 posts 916 karma points
    Mar 09, 2013 @ 19:19
    gary
    0

    Nice One!

    Must say, that code does look a lot cleaner and readable, I did think of suggesting something else but was fearful it would confuse more than it solved.

    Still haven't found some decent on Razor V2 MVC4, but came across this link that might interest you regarding <li> classes with razor and nulls.

    http://stackoverflow.com/questions/12694814/element-not-closed-error-after-upgrading-from-mvc3-to-mvc4

     

  • Proxicode 128 posts 324 karma points
    Aug 30, 2013 @ 08:41
    Proxicode
    0

    @Craig100 - That is a great solution to the problem and it helped me out big time!!  Thanks

Please Sign in or register to post replies

Write your reply to:

Draft