Copied to clipboard

Flag this post as spam?

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


  • Mikkel Johansen 12 posts 93 karma points
    Jul 24, 2016 @ 10:18
    Mikkel Johansen
    0

    Razor - Check for document type and create if statement

    Hi I need some help on how to check for a specific document type in Razor Script.

    I have a Page with several children, and every child has a different document type. Like so:

    • Page:
      • Child (Doc type 1)
      • Child (Doc type 2)
      • Child (Doc type 3)
      • Child (Doc type 4)

    What I am trying to do is, make a script that "lists all childs from current page"... Like the default template in Umbraco, named a like. But... I need to create an if statement, that formats the content different according to the document type.

    Like this:

    • If DocType1 - Do this
    • If DocType2 - Do this
    • If DocType3 - Do this

    and so on....

    Hope this makes any sense, and that someone can help me out here? :)

  • Mikkel Johansen 12 posts 93 karma points
    Jul 25, 2016 @ 08:15
    Mikkel Johansen
    0

    No one that can help me out here?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 25, 2016 @ 08:22
    Dennis Aaen
    0

    Hi Mikkel,

    Could you please try something like this and see if this solve what you are trying to do.

    @{ var selection = CurrentPage.Children.Where("Visible"); }
    
    @if (selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url">@item.Name</a>
                </li>
    
                    @if(item.DocumentTypeAlias == "Doctype1"){ 
                        // DO SOMETHING 
                    }
    
            }
        </ul>
    }
    

    Hope this helps,

    /Dennis

  • Mikkel Johansen 12 posts 93 karma points
    Jul 25, 2016 @ 09:30
    Mikkel Johansen
    0

    I get the following error:

    Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@". 
    

    And when I remove the "@" I get this error:

    Error occured
    
    d:\web\inmedia.dk\www\MacroScripts\636050433622081432_listSectionTypes.cshtml(4): error CS0103: The name 'CurrentPage' does not exist in the current context
    

    I am using Umbraco 6, if that has anything to say?

  • Mikkel Johansen 12 posts 93 karma points
    Jul 25, 2016 @ 09:47
    Mikkel Johansen
    0

    I tried using your example from above, like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    
    @* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
    @if (Model.Children.Where("Visible").Any())
    {
        <ul>            
            @* For each child page under the root node, where the property umbracoNaviHide is not True *@
            @foreach (var childPage in Model.Children.Where("Visible"))
            {
    
    
                    if(childPage.DocumentTypeAlias == "CallToAction"){ 
                        <li>Hello World!</li> 
                    } 
            }
        </ul>
    }
    

    But it seems like it doesn't work... I can save it without any errors, but it doesn't get the childPage with that documentTypeAlias.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 25, 2016 @ 12:13
    Dennis Aaen
    100

    Hi Mikkel,

    Okay you are using the old dynamic node razor.

    Could you then try to change:

         if(childPage.DocumentTypeAlias == "CallToAction"){ 
            <li>Hello World!</li> 
       } 
    

    To

         if(childPage.NodeTypeAlias == "CallToAction"){ 
            <li>Hello World!</li> 
       } 
    

    Hope this helps,

    /Dennis

  • Mikkel Johansen 12 posts 93 karma points
    Jul 25, 2016 @ 12:59
    Mikkel Johansen
    1

    Yes! that did the trick :)

    Once again, Dennis Aaen comes to the rescue... Thank you very much your help, Dennis.... I can always count on you, it seems :)

    BIG high five from me!

Please Sign in or register to post replies

Write your reply to:

Draft