Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Feb 09, 2015 @ 13:20
    René Andersen
    0

    Show data from root on frontpage

    Hi,

    The code below works without any problems but I have made it 2 years ago and I am thinking that maybe there is a better and easier way to do it. At the moment I am showing a slider on the frontpage and the data is stored like you see below:

    The code looks like this:

    @{
    var rootNode = new umbraco.MacroEngines.DynamicNode(-1);

    var dataNode = rootNode.Children.Items.SingleOrDefault(x => x.NodeTypeAlias == "Data");
    var dataNodeChildren = dataNode.Children as umbraco.MacroEngines.DynamicNodeList;

    var featuresBase = dataNodeChildren.Items.SingleOrDefault(x => x.NodeTypeAlias == "Slider");
    var featuresBaseChildren = featuresBase.Children as umbraco.MacroEngines.DynamicNodeList;

    foreach (dynamic testimonial in featuresBase.Children)
    {
    if (testimonial.NodeTypeAlias == "SliderItem")
    {
    <h1>@testimonial.GetProperty("text").Value</h1>
    }
    }
    }

    Could this be done easier and more clean?

    // René

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 09, 2015 @ 13:39
    Jan Skovgaard
    100

    Hi René

    I would say that you should consider using a content picker on your frontpage where you can point to the "Slider" of choice from the "Data" root.

    That way you just need to look at the id for the slider, instead of hardcoding it.

    This makes it possible to have more than one slider :)

    In regards to the Razor code...well it can most likely be refactored but I'll let some of the brighter Razor wizards in here get into details with that for now.

    /Jan

  • René Andersen 238 posts 684 karma points
    Feb 09, 2015 @ 14:09
    René Andersen
    1

    Hi Jan

    That's just perfect, your solution works great. :-)

    Thanks!

    The code now looks like this:

    @{
    if (CurrentPage.HasValue("contentPicker"))
    {
    var node = Umbraco.Content(CurrentPage.contentPicker);

    foreach (var childPage in node.Children.Where("Visible"))
    {
    <div class="item">
    <img src="@Umbraco.Media(childPage.GetPropertyValue("image")).Url" alt="@childPage.Name" class="full-width">
    <h1>@childPage.text</h1>
    </div>
    }
    }
    }

    // René

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 09, 2015 @ 15:00
    Jan Skovgaard
    0

    Hi René

    Glad that I could inspire you to writing some more efficient code :)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft