Copied to clipboard

Flag this post as spam?

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


  • bh 408 posts 1395 karma points
    Sep 06, 2019 @ 12:49
    bh
    0

    V8 Nested Content Template

    I've used Nested Content in the past like so:

    A Page loads a Slider as Nested Content the Slider loads Slides as Nested Content. The Slider has a template with HTML making that an object I can reuse with unique content on multiple Pages.

    In V8 I cannot put a template on my Slider and have it be Nested Content. So, my question is this...what's the best way to accomplish the same thing in V8 without manually duplicating the template markup everywhere I put a slider? Is it even possible?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 06, 2019 @ 13:36
    Nik
    0

    Hey BH,

    Not sure exactly what you mean, but I think what you are trying to do is possible just in a slightly different way.

    Could you elaborate on exactly what you mean so I can check?

    Thanks

    Nik

  • bh 408 posts 1395 karma points
    Sep 06, 2019 @ 14:05
    bh
    0

    In v7 I would have 3 DocTypes (Page, ncSlider, ncSlide), and 2 Templates (Page and Slider).

    Page would load Slider as Nested Content like so:

    @if(Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("homeSlider") != null){
        <div>
            @Html.Partial("ncSlider",Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("homeSlider"))
        </div>
    }
    

    Slider would load Slides as Nested Content like so:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedContent>>
    @{  
        var item = Model.FirstOrDefault();
        if(item == null)
        {return;}
        var slides = item.GetPropertyValue<IEnumerable<IPublishedContent>>("sliderSlides");
        foreach(var slide in slides){
            var title = slide.GetPropertyValue("ncSliderSlideTitle");
            var text = slide.GetPropertyValue("ncSliderSlideText");
            <div class="sliderTextSlide">
                <div class="title allcaps bold sansserif">@title</div>
                <div class="text">@text</div>
            </div>
        }
    }
    

    Hopefully that clarifies any ambiguity in my OP.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 06, 2019 @ 14:15
    Nik
    0

    Ahh okay yeah you can use the same approach with a small change.

    Nested Content blocks aren't IPublishedContent instead they are IPublishedElement instead. So where you have the former replace with the latter and see if that solves your issue :-)

    Nik

  • bh 408 posts 1395 karma points
    Sep 06, 2019 @ 14:26
    bh
    0

    @Nik the issue is in v8 I can't choose a DocType as Nested Content that has a Template (for example the ncSlider Template the 2nd code snip above...can't exist in v8). If it has a Template it can't be Nested Content.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 06, 2019 @ 14:50
    Nik
    0

    Hey bh,

    I'm not sure I get you to be honest.

    What I do with nested content, in both v7 and v8, is have them use partial views and not "templates"

    But looking at your code you are doing the same. Instead of having your template in the templates section for your slider move it to the Partial Views folder instead.

    Yes, in v8 you can't pick a template against a doc type that is of type Element, but you don't need to as when you are rendering it out you can tell it which partial view to use (which is what you appear to be doing anyway)

    For example, I have something similar to this:

    foreach(var item  in Model.MyNestedContentProperty)
    {
           var viewPath = $"AFolder/_{item.ContentTypeAlias}";
          if(Html.PartialViewExists(viewPath)) //This is an extension method I created
          {
               @Html.Partial(viewPath, item)
          }
    }
    

    What this does is look for views in /Partial/AFolder that have a name of the content type alias of my nested content object with a _ prefix. Then if it exists it renders it out.

    Templates are essentially only cshtml files. Umbraco just does some databasery stuff with them to link them to models, which you don't need to do for partial views :-)

    Does that help?

    Nik

  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Sep 06, 2019 @ 15:11
    Steve Megson
    0

    I don't think you even need to make those changes. The code above should work fine with no templates assigned to the ncSlider and ncSlide doc types. Since the code is loading the template by name, it doesn't depend on the template being linked to the doc type in the database.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 06, 2019 @ 15:13
    Nik
    0

    Hey Steve,

    Some code changes will be needed as by default UmbracoViewPage expects an IPublishedContent but in v8 all Element types are IPublishedElement to a small tweak to the top will need to be to change it to UmbracoViewPage<IPublishedElement> else errors are thrown.

    Nik

  • bh 408 posts 1395 karma points
    Sep 06, 2019 @ 15:18
    bh
    0

    Thanks @SteveMegson & @Nik...I'll give this a try and report back. I hope y'all are right!

Please Sign in or register to post replies

Write your reply to:

Draft