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?
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 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.
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 :-)
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.
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.
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?
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
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:
Slider would load Slides as Nested Content like so:
Hopefully that clarifies any ambiguity in my OP.
Ahh okay yeah you can use the same approach with a small change.
Nested Content blocks aren't
IPublishedContent
instead they areIPublishedElement
instead. So where you have the former replace with the latter and see if that solves your issue :-)Nik
@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.
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:
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
I don't think you even need to make those changes. The code above should work fine with no templates assigned to the
ncSlider
andncSlide
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.Hey Steve,
Some code changes will be needed as by default
UmbracoViewPage
expects anIPublishedContent
but in v8 all Element types areIPublishedElement
to a small tweak to the top will need to be to change it toUmbracoViewPage<IPublishedElement>
else errors are thrown.Nik
Thanks @SteveMegson & @Nik...I'll give this a try and report back. I hope y'all are right!
is working on a reply...