Copied to clipboard

Flag this post as spam?

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


  • syn-rg 282 posts 425 karma points
    Feb 17, 2014 @ 03:40
    syn-rg
    0

    Umbraco V 7.0.3 RelatedLinks

    I'm trying to create a RelatedLinks macro based on the default Related Links dataType.

    However, I'm getting the following error:

    Saving scripting file failed: The code 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.

    I can't see what I'm missing in my code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @*
        Model = The current page the macro is executed on
                @Model.bodyText
    
        Parameter = collection of parameter values passed from the macro
                @Paramter.myParam
    
        Library = utillity library with common methods
                @Library.NodeById(1233)
    *@
    
    
    
    @* The fun starts here *@
    @{
        if (Model.HasValue("relatedLinks") && Model.relatedLinks.Any())
        {
            <ul>
                @foreach (var item in Model.relatedLinks)
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? @umbraco.library.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = (item.newwindow.Equals("1")) ? " target=\"_blank\"" : string.Empty;
                    <li><a href="@linkUrl" @Html.Raw(linkTarget)>@item.title</a></li>
                }
            </ul>
        }
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 17, 2014 @ 09:47
    Jeavon Leopold
    100

    Hi JV,

    First thing is that is is highly recommended that you use a Partial View Macro rather then a Razor Macro with v7 for the best performance and also the original documentation snippet would work without modification. I'm guessing you are using WebForms templates rather than MVC?

    That all said, this snippet might work but you need to add @using Newtonsoft.Json.Linq For the Value converter although Model.relatedLinks is probably going to need to be converted to a JArray

    Jeavon

  • syn-rg 282 posts 425 karma points
    Feb 18, 2014 @ 04:10
    syn-rg
    0

    Hi Jeavon,

    I've added the following code as a Partial View Macro, however the "if" statement is working 100%. The macro is displaying on all of the child pages of the page which has the "relatedLinks" populated.

    e.g. About us (related links "Who we are" "Meet the team") --Who we are ---Where we work --Meet the team ---Join the team

    I've added related links to the "About us" page ("Who we are" "Meet the team"), the macro doesn't display on these pages, but it does on any other child page of "About us".

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @using Newtonsoft.Json.Linq
    @if (Model.Content.HasValue("relatedLinks"))
    {
        <div class="related-links">
            <h3>Related links</h3>
            <ul>
            @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
            {
                var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                var linkTarget = item.Value<bool>("newWindow") ? " target=\"_blank\"" : string.Empty;
                <li><a href="@linkUrl" @Html.Raw(linkTarget)>@(item.Value<string>("title"))</a></li>
            }
            </ul>
        </div>
    } 
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 18, 2014 @ 09:19
    Jeavon Leopold
    0

    Hi JV

    Glad it's mostly working for you.

    I don't quite follow what is happening for you now, but this snippet will display the related links from the property on the current page only.

    Have you checked to ensure that the property alias is the exactly the same on all document types?

    Jeavon

  • syn-rg 282 posts 425 karma points
    Feb 19, 2014 @ 03:08
    syn-rg
    0

    Hi Jeavon,

    Yes the property alias is the same for all document types.

    The link UL is empty though, when it appears on the pages it shouldn't.

    <div class="related-links">
            <h3>Related links</h3>
            <ul>
            </ul>
        </div>
    

    JV

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 19, 2014 @ 10:51
    Jeavon Leopold
    0

    Ah ok, I think it has a empty Json string in there, I would probably go with this:

    @{
        if (Model.Content.HasValue("relatedLinks"))
        {
            var relatedLinks = Model.Content.GetPropertyValue<JArray>("relatedLinks");
            if (relatedLinks.Any())
            {
                <ul>
                    @foreach (var item in relatedLinks)
                    {
                        var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                        var linkTarget = item.Value<bool>("newWindow") ? " target=\"_blank\"" : string.Empty;
                        <li><a href="@linkUrl" @Html.Raw(linkTarget)>@(item.Value<string>("caption"))</a></li>
                    }
                </ul>
            }
        }
    }  
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 19, 2014 @ 10:56
    Jeavon Leopold
    1

    Given it contains only "[]" you could do something like this instead:

    @{      
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? " target=\"_blank\"" : string.Empty;
                    <li><a href="@linkUrl" @Html.Raw(linkTarget)>@(item.Value<string>("caption"))</a></li>
                }
            </ul>
        }
    }  
    
  • syn-rg 282 posts 425 karma points
    Feb 20, 2014 @ 02:06
    syn-rg
    0

    That's better! Thanks for you help Jeavon!

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 20, 2014 @ 20:30
    Jeavon Leopold
    0

    You're welcome!

    I have slightly improved this snippet and updated the documentation accordingly

    @using Newtonsoft.Json.Linq
    @{      
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                }
            </ul>
        }
    }  
    
  • Ehsan 8 posts 30 karma points
    Nov 28, 2014 @ 17:50
    Ehsan
    0

    everyone talks about how to retrieve(get) data from a relatedLinks property,, but what about how to programatically set a relatedLinks property?? I've tried so much but no success :s

Please Sign in or register to post replies

Write your reply to:

Draft