Render Stacked Content in frontend - partial view examples
Hi,
Stacked Content is yet another great content builder, but the lack of documentation makes it hard for non-developers like myself.
Is there any friendly umbracians out there, who can give one or a few examples on how to render the stacked content stack in frontend via a Partial View? I am totally blank on how to do this the best/easiest way?
Essentially each 'stacked item' in your stacked content implementation is represented by an IPublishedContent object. Which means you can work with each item, in the same way that you would with a published page in Umbraco.
So if you have a StackedContent property called say 'StackedItems' you could get a reference in your template to each repeated item like so:
var stackedItems = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("stackedItems");
and then loop through this collection of stacked items to build up your page:
@if (stackedItems!=null && stackedItems.Any()){
foreach (var stackedItem in stackedItems){
<li>
//each repeating stackedItem has the same properties as Model.Content would... eg
@stackedItem.Name
//if you have different types of documents stacked, you can read the alias of the current item being written out
@stackedItem.DocumentTypeAlias
//you can read the values from the stacked content item in the same way
@stackedItem.GetPropertyValue<string>("title")
</li>
}
}
If I've got quite a few different types of stacked items in my stack, or if I use the same types of content on different stacks across the site, then I'll tend to create a 'partial view' for each type of stacked content item, and call that within the loop, just to neaten things up a bit.
This gives one place to update implementation if it's the same and used in different stacks, but also makes it easier to read if you have more than 3 different content types stacked...
That makes perfect sense, didn't think is was that easy. It basically means that I can reuse my Nested Content snippets, with only few changes.
Thanks for your answer Marc! :)
Render Stacked Content in frontend - partial view examples
Hi,
Stacked Content is yet another great content builder, but the lack of documentation makes it hard for non-developers like myself.
Is there any friendly umbracians out there, who can give one or a few examples on how to render the stacked content stack in frontend via a Partial View? I am totally blank on how to do this the best/easiest way?
Thanks in advance! :)
Hi Hundebol
Essentially each 'stacked item' in your stacked content implementation is represented by an IPublishedContent object. Which means you can work with each item, in the same way that you would with a published page in Umbraco.
So if you have a StackedContent property called say 'StackedItems' you could get a reference in your template to each repeated item like so:
and then loop through this collection of stacked items to build up your page:
If I've got quite a few different types of stacked items in my stack, or if I use the same types of content on different stacks across the site, then I'll tend to create a 'partial view' for each type of stacked content item, and call that within the loop, just to neaten things up a bit.
eg
and then in a 'stacked' folder inside view/partial I'd create a partial view following the convention
type-MyContentItemAlias.cshtml
and have an inherits at the top of the partial as
This gives one place to update implementation if it's the same and used in different stacks, but also makes it easier to read if you have more than 3 different content types stacked...
That makes perfect sense, didn't think is was that easy. It basically means that I can reuse my Nested Content snippets, with only few changes. Thanks for your answer Marc! :)
is working on a reply...