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>
}
}
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
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".
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
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:
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 JArrayJeavon
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".
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
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.
JV
Ah ok, I think it has a empty Json string in there, I would probably go with this:
Given it contains only "[]" you could do something like this instead:
That's better! Thanks for you help Jeavon!
You're welcome!
I have slightly improved this snippet and updated the documentation accordingly
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
is working on a reply...