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
    Apr 07, 2015 @ 14:22
    Steve
    0

    Syntax for getting the URL Property of a Node

    It's been a while since I've had to write a Razor macro that uses a content picker as a property and I can't seem to get the syntax correct to get the url on the <a href>. Here is my code:

    @{
            var page = Model.AncestorOrSelf("PresidentsHomePage");
    
            if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){
    
                <div id="featureBoxWrap">
    
                <a class="featureBox" href="@page.featureBox1Url">
            @if(page.HasValue("featureBox1Headline")){
                <div class="featureHeadline">
                    @page.featureBox1Headline
                    <hr>
                    </div>
                    }   
            @if(page.HasValue("featureBox1Image")){
                var imageBox1 = @Model.Media("featureBox1Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox1Content")){
                <div class="featureContent">
                    @page.featureBox1Content
                    </div>
                    }   
                </a>
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 07, 2015 @ 14:34
    Dennis Aaen
    0

    Hi Steve,

    Try to see this documentation on how to get the url from a content picker. https://our.umbraco.org/Documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Content-Picker

    An example.

    @{
      if (Model.HasValue("contentPicker")){
        var node = Library.NodeById(Model.contentPicker);
        <a href="@node.Url">@node.Name</a>
      }
    }

    Hope this helps,

    /Dennis

  • Steve 472 posts 1216 karma points
    Apr 07, 2015 @ 15:55
    Steve
    0

    Thanks Dennis! This works great, except can I do this without a test for the value of contentPicker without risking exceptions? I really don't care if the user provides a url or not.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 07, 2015 @ 16:11
    Dennis Aaen
    0

    Hi Steve,

    You could give it a try just to use this. And then try not to add a link to one of the elements and see if you get any risking exceptions

    var node =Library.NodeById(Model.contentPicker);
    <a href="@node.Url">@node.Name</a>

    Else then you could make an if else statement.

    if(Model.HasValue("contentPicker")){
       
    var node =Library.NodeById(Model.contentPicker);
       
    <a href="@node.Url">@node.Name</a>
    }else{
        <a href="">@node.Name</
    a>
    }

    /Dennis

  • Steve 472 posts 1216 karma points
    Apr 07, 2015 @ 16:23
    Steve
    0

    Looks like I will have to try the if/else because the macro fails to load without the if statement.

  • Steve 472 posts 1216 karma points
    Apr 07, 2015 @ 16:31
    Steve
    0

    Okay, now I need to show you more of the code, because it says it's expecting a "}" after my first <a> line, but there is one right befor the "else". What's going on?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
        @{
            var page = Model.AncestorOrSelf("PresidentsHomePage");
    
    
            if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){
    
                <div id="featureBoxWrap">
            @if(page.HasValue("featureBox1Url")){
                    var node = Library.NodeById(Model.featureBox1Url)
                <a class="featureBox" href="@node.Url">
                        } else {
                        <a class="featureBox" href=""> 
            @if(page.HasValue("featureBox1Headline")){
                <div class="featureHeadline">
                    @page.featureBox1Headline
                    <hr>
                    </div>
                    }   
    
            @if(page.HasValue("featureBox1Image")){
                var imageBox1 = @Model.Media("featureBox1Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox1Content")){
                <div class="featureContent">
                    @page.featureBox1Content
                    </div>
                    }   
                </a>
                    }
    }
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 07, 2015 @ 19:39
    Dennis Aaen
    0

    Hi Steve,

    What if you do something like this would this work for you.

    @inherits umbraco.MacroEngines.DynamicNodeContext

        @{
            var page = Model.AncestorOrSelf("PresidentsHomePage");


            if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){
                <div id="featureBoxWrap">
                     @if(page.HasValue("featureBox1Url")){
                        var node = Library.NodeById(Model.featureBox1Url);
                          <a class="featureBox" href="@node.Url"></a>
                            }else{
                     
                               <a class="featureBox" href="">
            @if(page.HasValue("featureBox1Headline")){
                <div class="featureHeadline">
                    @page.featureBox1Headline
                    <hr>
                    </div>
                    }  

            @if(page.HasValue("featureBox1Image")){
                var imageBox1 = @Model.Media("featureBox1Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox1Content")){
                <div class="featureContent">
                    @page.featureBox1Content
                    </div>
                    }  
                </a>
                     
               }
                     
                </div>
           
            }
    }

    Hope this helps,

    /Dennis

  • Steve 472 posts 1216 karma points
    Apr 07, 2015 @ 20:20
    Steve
    0

    Well Dennis, I've done something like this, but if I add an <a> tag anywhere in any of the "featureBox" Content areas it creates another <a class="featureBox"> arround the link. Here is my code and the page:  Page( https://edit-wwwprep.rose-hulman.edu/32742.aspx )

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
            var page = Model.AncestorOrSelf("PresidentsHomePage");
    
    
    if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){
    
    <div id="featureBoxWrap">
            @if(page.HasValue("featureBox1Url")){
                    var node = Library.NodeById(@Model.featureBox1Url);
                        <a class="featureBox" href="@node.Url">
            @if(page.HasValue("featureBox1Headline")){
                        <div class="featureHeadline">
                        @page.featureBox1Headline
                            <hr>
                        </div>
            }   
    
            @if(page.HasValue("featureBox1Image")){
                var imageBox1 = @Model.Media("featureBox1Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
    
            @if(page.HasValue("featureBox1Content")){
                <div class="featureContent">
                    @page.featureBox1Content
                    </div>
            }
                        </a>
                            } else {
                <div class="featureBox" >
            @if(page.HasValue("featureBox1Headline")){
                <div class="featureHeadline">
                    @page.featureBox1Headline
                    <hr>
                    </div>
            }   
    
            @if(page.HasValue("featureBox1Image")){
                var imageBox1 = @Model.Media("featureBox1Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox1Content")){
                <div class="featureContent">
                    @page.featureBox1Content
                    </div>
            }   
                </div>
                    }
    
            @if(page.HasValue("featureBox2Url")){
                    var node = Library.NodeById(@Model.featureBox2Url);
                        <a class="featureBox" href="@node.Url">
    
            @if(page.HasValue("featureBox2Headline")){
                <div class="featureHeadline">
                    @page.featureBox2Headline
                    <hr>
                    </div>
            }   
            @if(page.HasValue("featureBox2Image")){
                var imageBox2 = @Model.Media("featureBox2Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox2Content")){
                <div class="featureContent">
                    @page.featureBox2Content
                    </div>
            }   
                        </a>
                } else {
                    <div class="featureBox">
            @if(page.HasValue("featureBox2Headline")){
                <div class="featureHeadline">
                    @page.featureBox2Headline
                    <hr>
                    </div>
            }   
            @if(page.HasValue("featureBox2Image")){
                var imageBox2 = @Model.Media("featureBox2Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox2Content")){
                <div class="featureContent">
                    @page.featureBox2Content
                    </div>
            }   
                    </div>
    
                }
    
            @if(page.HasValue("featureBox3Url")){
                    var node = Library.NodeById(@Model.featureBox3Url);
                        <a class="featureBox" href="@node.Url">
    
            @if(page.HasValue("featureBox3Headline")){
                <div class="featureHeadline">
                    @page.featureBox3Headline
                    <hr>
                    </div>
            }   
            @if(page.HasValue("featureBox3Image")){
                var imageBox3 = @Model.Media("featureBox3Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox3Content")){
                <div class="featureContent">
                    @page.featureBox3Content
                    </div>
            }   
                </a>
                    } else {
                <div class="featureBox">
            @if(page.HasValue("featureBox3Headline")){
                <div class="featureHeadline">
                    @page.featureBox3Headline
                    <hr>
                    </div>
            }   
            @if(page.HasValue("featureBox3Image")){
                var imageBox3 = @Model.Media("featureBox3Image");
                    <div class="featureImage">
                    <img src="/[email protected]&width=263" />
                    </div>
            }
            @if(page.HasValue("featureBox3Content")){
                <div class="featureContent">
                    @page.featureBox3Content
                    </div>
            }   
                </div>
            }
    
    
        </div>            
            }
    }
    
  • Steve 472 posts 1216 karma points
    Apr 07, 2015 @ 21:48
    Steve
    0

    The big thing for me is correctly wrapping the featureBoxUrl, featureBoxImage, featureBoxHeadline and featureBoxContent all within the <a class="featureBox">, but for some reason, a user decides to put an in-line link within the "featureBoxContent" field on the page the razor doesn't wrap the outer <a> correctly, it in fact, ends it after the "featureBoxContent" field. Any suggestions about correcting this will be greatly appreaciated!!

Please Sign in or register to post replies

Write your reply to:

Draft