I'm trying to dynamically (in the template) link to another node in the same site based on the content of a custom datatype. What I have now doesn't work, and I'm almost POSITIVE that my syntax is horrendous...
@{
var reviewer = Umbraco.Content(1096);
var page = reviewer.Children;
<p>Reviewed by <a href="@page.reviewerName.Parent.Url">@CurrentPage.reviewedBy</a>, @CurrentPage.reviewerTitle for...</p>
}
So... What's the simplest way to do this? Partial Macro? I'm at a loss here...
First - reviewer.Children returns list of child nodes, not just one page, so you have to filter this list and get only one page.
How can we define what page we need from Choildren?
Second - page.reviewerName.Parent.Url, page is dynamic type, and we can get data from reviewerName property like that, but we can't get Parent and Url of parent.
We have to get reviewerName node first -
var reviewerNameNode = Umbraco.Content(page.reviewerName);
var url = reviewerNameNode.Parent.Url;
@{
var reviewers = Umbraco.Content(1096);
foreach(var reviewer in reviewers.Children)
{
if (@reviewer.reviewerName == @CurrentPage.reviewedBy)
{
<p>Reviewed by <a href="@reviewer.Url">@CurrentPage.reviewedBy</a>, @CurrentPage.reviewerTitle for John Garcia's THE COLUMN</p>
}
}
}
Not elegant, but works... Thanks again though Alex!
If you would like to write it in more elegant way you can do it like this:
var reviewers = Umbraco.Content(1096) as IPublishedContent;
if (reviewers != null)
{
var specificReviewer = reviewers.Children.FirstOrDefault(c => c.GetPropertyValue<string>("reviewerName") == CurrentPage.reviewedBy);
if (specificReviewer != null)
{
<p>Reviewed by <a href="@specificReviewer.Url">@CurrentPage.reviewedBy</a>, @CurrentPage.reviewerTitle for John Garcia's THE COLUMN</p>
}
}
Dynamic Link to Node based on Property
I'm trying to dynamically (in the template) link to another node in the same site based on the content of a custom datatype. What I have now doesn't work, and I'm almost POSITIVE that my syntax is horrendous...
So... What's the simplest way to do this? Partial Macro? I'm at a loss here...
Umbraco Version 7.5.2
Hi Aaron,
First - reviewer.Children returns list of child nodes, not just one page, so you have to filter this list and get only one page.
How can we define what page we need from Choildren?
Second - page.reviewerName.Parent.Url, page is dynamic type, and we can get data from reviewerName property like that, but we can't get Parent and Url of parent.
We have to get reviewerName node first -
Thanks,
Alex
Thanks Alex! I'll change/add it around and let get back for the update! Fundamentals... How bourgeois....
Okay, I SUPER cheated, but it works.... I used:
Not elegant, but works... Thanks again though Alex!
Hi Aaron,
Glad to help you.
Have a nice evening.
Thanks,
Alex
Hello Aaron,
If you would like to write it in more elegant way you can do it like this:
Hi Raibow,
One small small remark to your code :) Please, look at this line:
Umbraco.Content returns dynamic, but Umbraco helper has TypedContent method which returns IPublishedContent, so you have to fix this line to:
Thanks,
Alex
Nice! Thanks Raibow and again Alex! That's WAY more direct!
Hi Alex,
thank you for your remark.
It is much more convinient or even more desirable to use
TypedContent
helper but I think both are correct :)is working on a reply...