Copied to clipboard

Flag this post as spam?

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


  • Charles Roper 48 posts 200 karma points
    Feb 26, 2016 @ 12:11
    Charles Roper
    0

    Abstracting a small function

    Hey gang,

    So my knowledge is progressing and I'm at the stage now where I'm trying to DRY my Razor views and could use some advice.

    Background: I've got a bunch of nodes (let's call them blog posts) where the editor can set a date or leave it blank. If the editor specifies a date, then I output that in the view, otherwise I make use of the CreateDate property.

    Here's the little snippet I'm using to do that:

    var postDate = post.HasValue("postDate")
        ? post.GetPropertyValue<DateTime>("postDate")
        : post.CreateDate;
    

    I'm using this little snippet all over the place now and so it seem a good candidate for abstracting into its own helper function somewhere. My question is: what would be the best way to do this? Razor helper or some other means? I want to end up being able to do something like this:

    var postDate = getPostDate(post, "postDate");
    

    Presumably then I'd be able to use it in situations like this:

    var posts = Umbraco.TypedContentAtRoot()
        .First()
        .Descendants()
        .OfTypes("postPage")
        .OrderByDescending(x => x.HasValue("postDate")
            ? x.GetPropertyValue<DateTime>("postDate")
            : x.CreateDate);
    

    It seems a pretty trivial thing, but don't know where to start looking. :)

    Many thanks in advance!

    Charles

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 26, 2016 @ 12:17
    Dave Woestenborghs
    100

    Hi Charles,

    I would create a extension method for it.

    Something like this :

    public static class PublishedContentExtensions
        {
            public static DateTime GetPostDate(this IPublishedContent content)
            {
                var defaultDate = content.CreateDate;
    
                if(content.HasProperty("postDate") && content.HasValue("postDate")) {
                    return content.GetPropertyValue<DateTime>("postDate");
                }
    
                return defaultDate;
            }
    
    
        }
    

    Than you can do :

    var posts = Umbraco.TypedContentAtRoot()
        .First()
        .Descendants()
        .OfTypes("postPage")
        .OrderByDescending(x => x.GetPostDate());
    

    Dave

  • Charles Roper 48 posts 200 karma points
    Feb 26, 2016 @ 13:06
    Charles Roper
    0

    That's awesome, Dave, thanks so much. Just what I am looking for.

    One further question, though: where exactly do I put this code in my solution?

    Thanks again.

    Charles

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 26, 2016 @ 13:33
    Dave Woestenborghs
    1

    If you have a VS Solution that you compile ? Than you can put it where you want.

    Otherwise putting it in the App_Code folder should work

    Dave

  • Charles Roper 48 posts 200 karma points
    Feb 26, 2016 @ 14:07
    Charles Roper
    0

    Yes, I do have a VS solution. Is there any recommend place to put it in? Some sort of convention generally used?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 26, 2016 @ 14:08
    Dave Woestenborghs
    1

    I normally put it in a folder called extension methods.

    Dave

  • Charles Roper 48 posts 200 karma points
    Feb 26, 2016 @ 14:16
    Charles Roper
    0

    Great, thanks very much!

Please Sign in or register to post replies

Write your reply to:

Draft