How to define a template for a content type container and containable

Jan 17, 2011 at 10:31 PM


I am trying to have a content type for which both container and containable parts are defined. However, as soon as it contains one element, the template I have defined is not applied anymore on this item. Is there any way to override this behavior ?


Jan 17, 2011 at 10:35 PM

Can you give more details on what you are doing and what happens?

Jan 18, 2011 at 5:25 AM
I have created a content type named activity with the content parts common, route, menu, body, container and containable (without writing one line of c#). when I create a theme, I would like to template it, then I create a template named Content-Activity.Detail.cshtml that I add to the folder views/item. The template is being applied like a charm while my activity does not contain any item. As soon as my activity contains one item, the template I have defined is not applied anymore and I only have the contained items of my activity displayed. These items can be templated using the template Content-MyContaineditemType.Summary.cshtml. but the template of the container is not being used anymore and the body of the container is not displayed.
Jan 18, 2011 at 3:09 PM

Sorry Nicolas, it's not yet clear ;) But I assume that we should try to create a List, add some content items to it, and try to customize the templates for the list, the summaries and the details views to see if there is an issue here.

We'll do it and give you our results.

Jan 19, 2011 at 5:38 AM

Actually, I think the issue is that my content type Activity is a kind of list, because I added the container part on it.

The test you should make should be :

  • create a content type A, 
  • add it the content parts containable, body, route, container and common. 
  • create a instance of A
  • Create a template for a detail of A
  • Display the instance of A (Result OK : the template is used)
  • create a new content type B,
  • add it the content parts containable, body, Route, and common
  • create an instance of B where the parent is an instance of A previously created
  • re-display the instance of A (Result NOK : the template is not used)

The context :

I'm trying to make the evaluant web site with orchard. I have the activities Services, Training and Innovation. When I click on Services, I want to display a list of Poles (Custom Development, ...). Pole is another content type.

While my Activity Services do not have Poles, my template Content-Activity.Detail.cshtml is being used and that's what's expected.

When I add instances of Poles to my Services Activity, the template Content-Activity.Detail.cshtml is not begin used anymore. I still can template each instance of Pole using a template named Content-Pole.Summary.cshtml. But I still would like to be able to template the container of Poles : the Services activity.


To make things more understandable, I have put Content types in bold and content type instances in italic. Hope this helps ;)

Feb 9, 2011 at 7:27 AM
Edited Feb 9, 2011 at 7:51 AM

I'm trying to do a similar thing by having a list of categories and inside each category I'd like to have forum posts. I wonder if I create my own Summary view for forum posts can I access the data for the category from within the view model or some other way? This would enable me to create a great list of posts with the category information as well.

Feb 13, 2011 at 6:15 AM
Edited Feb 24, 2011 at 10:33 AM

<Post Removed>, this was not a good way to do it. See below for better solution.

Feb 24, 2011 at 3:28 AM

I left something out of the code above, you have add the containerShape to the final shape by adding .Container(containerShape)

I agree this isn't the right way to do this in Core, but I'm still having trouble getting this to work in another module with a handler. I'm not sure what is happening when I try to create this with a handler, but I think they should consider doing something in the next version of Orchard to allow us to show the container as well as the contained items on the same page. This might not be the correct solution, but I believe this is the expected behavior of containers and it is lacking in the current release.

Feb 24, 2011 at 4:27 AM

Not with a handler. Just define your own controller. This must not be done in core.

Feb 24, 2011 at 6:06 AM

Yes a controller is what I meant. That is what I tried. But why would this not at some point be incorporated into core? Isn't it the intended functionality of containers/containable?

Feb 24, 2011 at 6:07 AM

Then you may want to submit a patch.

Feb 24, 2011 at 10:29 AM
Edited Feb 24, 2011 at 10:33 AM

So what I think is the right thing to be doing here is to build a summary display for the container and add it to the view model then display it in Display.cshtml. The only problem here is that it is the summary, which might be preferred actually, but doing it without summary caused it to crash in a very strange place. I suspect its trying to maybe show the contained items more than once. So with this new way its a lot less code and I think it would be safe to make a patch for it. So instead of all that craziness above all you need to do is add a line right before the call to Shape.ViewModel() in ItemController.cs and add the containerShape to the ViewModel, so it becomes this:


var containerShape = _contentManager.BuildDisplay(container, "Summary");

dynamic viewModel = Shape.ViewModel()


Then just add @Display(Container) to the top of Display.cshtml. So there is no Container.cshtml needed as it is not creating a new shape. And this will allow all your parts and fields to display as usual and you'll get the edit box when logged in as admin.

This seems like the correct way to do it, although I'm not sure if Summary is what people would expect. I'm guessing because the item controller is a controller instead of a ContentPartDriver for ContainerPart we don't get the normal way of doing things here. I suspect there was some kind of loop or infinite recursion that happened when doing it the normal way, or it doesn't work correctly in every case. At any rate, that would be the preferred way to do this for consistency, but if there is a reason why that cannot be done, I think this is the next best thing.

Feb 24, 2011 at 7:00 PM

OK, thanks for the investigation and code. Can you please open a bug in the issue tracker for that, point to this thread from there and maybe even submit a patch?


Apr 21, 2011 at 3:58 AM

Hi all,

it's been a long time, but I did not have time before to answer. The solution proposed by themicster does not fit entirely my needs : I had to change the code to this :


var containerShape = _contentManager.BuildDisplay(container, "Detail");

dynamic viewModel = Shape.ViewModel()


But now, with the new version of Orchard, I used shape tracing and now know that the issue is my template is not used at all if I do not use this code in the Item controller. However, where it does not solve my issue, this is because with this code, I am not able to choose where or how to display my ContentItems. Any of you guys have an idea ? Need more explanation ?


Nicolas Penin