Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    May 20, 2015 @ 16:59
    Steve
    0

    Nested " if " In Navigation

    I need to test for a doctype of "GroupingFolder" when traversing the nodes in my navigation, but I can't seem to get the syntax correct. Could someone take a look to see what I am missing? 

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 4 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 6 : int.Parse(Parameter.finishLevel);
    var page = Model.AncestorOrSelf(StartLevel);
        if(page != null)
        {
        @traverse(page, StartLevel, FinishLevel);   
        }                
    }   
    
    @helper traverse(dynamic page, int StartLevel, int FinishLevel)
    {
        <ul class="nodeList">
            @foreach (var node in page.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\" || NodeTypeAlias == \"GroupingFolder\""))
                {
                var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
                var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                    if(node.Where("Visible && NodeTypeAlias == \"GroupingFolder\"")){
                        <li @childActive @levelOne>@itemName</li>
                    } else {
                <li @childActive @levelOne ><a href="@link" @target>@itemName</a>
    
        @if(node.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\"").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
              { 
              @traverse(node, StartLevel, FinishLevel);
              }
                </li>
                    } }
        </ul>     
    }
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 20, 2015 @ 18:07
    Jan Skovgaard
    0

    Hi Steve

    What does the error message say? Does it provide a hint about where in the code things seem to go wrong?

    /Jan

  • Steve 472 posts 1216 karma points
    May 20, 2015 @ 18:10
    Steve
    0

    It won't save the code as is. Says "The helper block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. "

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 20, 2015 @ 18:17
    Jan Skovgaard
    0

    Hi Steve

    Ok, seems there is one too many } signs in the if/else block you have.

    Try copy/pasting this and see if it works

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 4 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 6 : int.Parse(Parameter.finishLevel);
    var page = Model.AncestorOrSelf(StartLevel);
    
        if(page != null)
        {
        @traverse(page, StartLevel, FinishLevel);   
        }
    
    }   
    
    @helper traverse(dynamic page, int StartLevel, int FinishLevel)
    {
        <ul class="nodeList">
            @foreach (var node in page.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\" || NodeTypeAlias == \"GroupingFolder\""))
                {
                var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
                var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
    
                    if(node.Where("Visible && NodeTypeAlias == \"GroupingFolder\"")){
                        <li @childActive @levelOne>@itemName</li>
                    } else {
                <li @childActive @levelOne ><a href="@link" @target>@itemName</a>
    
                        @if(node.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\"").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
                        { 
                            @traverse(node, StartLevel, FinishLevel);
                        }
                </li>
                }
        </ul>     
    }
    

    Does this help?

    /Jan

  • Steve 472 posts 1216 karma points
    May 20, 2015 @ 18:22
    Steve
    0

    No, now it states that it is expecting a "}" at the last line.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 20, 2015 @ 18:29
    Jan Skovgaard
    0

    Hi Steve

    Ok - Not sure what it could be then - Just wondering if you're using Visual Studio for writing the code? Or are you doing it in another editor? Just wondering if you have intellisense - If not it might help.

    Otherwise I usually use an approach where I uncomment all the code and then make sure I don't see an error message. Then I start commenting in parts of the code and usually it's pretty quick to figure out where the problem is when progressing like this and have it fixed. Don't know if it's an approach that can work for you too?

    /Jan

  • Steve 472 posts 1216 karma points
    May 20, 2015 @ 18:47
    Steve
    0

    I've got Visual Studio for the web, but I don't think there is a way to check Razor syntax.

  • Steve 472 posts 1216 karma points
    May 21, 2015 @ 14:47
    Steve
    0

    I've got it in Visual Studio and It keeps saying the same thing as the Umbraco editor " } expected line 41", which is the last line, but adding a second closing bracket doesn't fix the error.

  • Steve 472 posts 1216 karma points
    May 21, 2015 @ 17:07
    Steve
    0

    If I try to comment out lines as you suggest using @* in the Umbraco code editor I always get this error: Keyword, identifier, or string expected after verbatim specifier: @

     

  • Steve 472 posts 1216 karma points
    May 21, 2015 @ 21:22
    Steve
    0

    Okay, I couldn't get the if syntax to work, so I used a terinary, but I wonder if there might be something wrong with my syntax even though it works, because It is showing my opening and closing { } in red. Can someone take a look to see if I am missing something? Thanks!

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 4 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 6 : int.Parse(Parameter.finishLevel);
    var page = Model.AncestorOrSelf(StartLevel);
        if(page != null)
        {
        @traverse(page, StartLevel, FinishLevel);   
        }                
    }   
    
    @helper traverse(dynamic page, int StartLevel, int FinishLevel)
    {
    
        <ul class="nodeList">
            @foreach (var node in page.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\" || NodeTypeAlias == \"GroupingFolder\""))
                {
                var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
                var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
    
                <li @childActive @levelOne ><a @target href="@(node.Where("NodeTypeAlias == \"GroupingFolder\"") ? @node.Down().Url : @link)" >@itemName</a>
    
        @if(node.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\" || NodeTypeAlias == \"GroupingFolder\"").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
              { 
              @traverse(node, StartLevel, FinishLevel);
              }
                </li>
                }   
        </ul>         
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft