Copied to clipboard

Flag this post as spam?

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


  • Aaron Lett 21 posts 72 karma points
    Nov 30, 2016 @ 23:03
    Aaron Lett
    0

    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...

    @{
        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...

    Umbraco Version 7.5.2

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 30, 2016 @ 23:16
    Alex Skrypnyk
    100

    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 -

    var reviewerNameNode = Umbraco.Content(page.reviewerName);
    var url = reviewerNameNode.Parent.Url;
    

    Thanks,

    Alex

  • Aaron Lett 21 posts 72 karma points
    Dec 01, 2016 @ 16:27
    Aaron Lett
    0

    Thanks Alex! I'll change/add it around and let get back for the update! Fundamentals... How bourgeois....

  • Aaron Lett 21 posts 72 karma points
    Dec 01, 2016 @ 16:58
    Aaron Lett
    0

    Okay, I SUPER cheated, but it works.... I used:

    @{
        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!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Dec 01, 2016 @ 17:00
    Alex Skrypnyk
    0

    Hi Aaron,

    Glad to help you.

    Have a nice evening.

    Thanks,

    Alex

  • raibow 10 posts 80 karma points
    Dec 01, 2016 @ 20:02
    raibow
    0

    Hello Aaron,

    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>
        }
    }
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Dec 01, 2016 @ 22:15
    Alex Skrypnyk
    0

    Hi Raibow,

    One small small remark to your code :) Please, look at this line:

    var reviewers = Umbraco.Content(1096) as IPublishedContent;
    

    Umbraco.Content returns dynamic, but Umbraco helper has TypedContent method which returns IPublishedContent, so you have to fix this line to:

    var reviewers = Umbraco.TypedContent(1096);
    

    Thanks,

    Alex

  • Aaron Lett 21 posts 72 karma points
    Dec 01, 2016 @ 22:33
    Aaron Lett
    0

    Nice! Thanks Raibow and again Alex! That's WAY more direct!

  • raibow 10 posts 80 karma points
    Dec 02, 2016 @ 09:55
    raibow
    0

    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 :)

Please Sign in or register to post replies

Write your reply to:

Draft