List Rendering Template for Custom Content Type

Topics: Customizing Orchard
Editor
Jun 2, 2011 at 8:28 PM

I understand how to create a template for a content type to control its rendering in a Summary or Detail view. The problem I am looking at is how do I control the way a List (Containable) will render based on the content type it holds? For Blog Posts I know we can do Parts.Blogs.BlogPost.List.cshtml to control the rendering of this list, but what about for a custom content type?

I currently used shape tracer to look for some alternates and for my Widget>Container, which is a list of my custom type "News Item" I would think I could create a template file named:

Widgets.Container.NewsItem.List.cshtml

or

Parts.NewsItem.List.cshtml

 

Any thoughts on this?

Coordinator
Jun 2, 2011 at 8:40 PM

Well, in most cases it's the summary template that you want to change. On the list rendering itself, the right semantics are UL/LI so the only thing that would vary should be classes, in which case you might want to take a look at Vandelay.Classy for more styling options (see http://weblogs.asp.net/bleroy/archive/2011/05/06/even-better-customizability-in-orchard.aspx).

If you need more than that, I think you are missing an alternate, which is nothing you can't fix with a little code (see http://weblogs.asp.net/bleroy/archive/2011/01/10/orchard-shapeshifting.aspx).

Jun 2, 2011 at 8:41 PM

You can just create your own list content types with different names, and specify specific types they can hold. Then use the different list types to override the template. You just need ContainerPart for a content type to be a list.

Editor
Jun 2, 2011 at 9:06 PM

Thanks Bertrand, I get what your saying.

Random Pete I also get what your saying as well. I would then get to use an Alternate for the Parts.NewsLists.Lists.cshtml rendering for that content type that is a containable correct?

My exact situation is less about the rendering of the UL>LI structure and more about having access to the list at render time. I want to have a "Month" navigational structure that the user can toggle and based on the querystring I will only display News Items from that month. If I can access the List Rendering portion then I can add that nav structure above the list and also only render children who fall in those months.

It seems like creating a new Content Type that has containable as a part and then creating a template for its List rendering should do it. Do you happen to know the template file name syntax I should use? If I have a Content Type of NewsList which is composed of "NewsItems"?

Parts.NewsList.List.cshtml?

Coordinator
Jun 2, 2011 at 9:37 PM

Ah but then you should not be doing that filtering at render-time. This should be done from the driver or controller. It has nothing to do with rendering and everything to do with data.

Editor
Jun 2, 2011 at 9:44 PM

I can't override the controller or driver for custom content types, correct? This is by default in the Core list rendering code. Is there a way to cleanly override it without modifying the Core code?

Coordinator
Jun 2, 2011 at 9:54 PM

Of course you can provide your own controller or driver. What would prevent you from doing that? As for rendering, I've already posted a link above that shows how to override list rendering.

Editor
Jun 2, 2011 at 10:02 PM

Do I need to create my own Shape from scratch or can I simply put a Controller or Driver file in my Themes Directory with a specific naming context for my List of NewsItems? If you could point me to a good walk through over overriding a Controller for List rendering of a Custom Content Type that would be very helpful.

 

Thanks!

Coordinator
Jun 2, 2011 at 10:09 PM

You can do anything. A theme is probably not the best place to put that sort of code but if you must, it's possible if you put a csproj in your theme. Note that you wouldn't "override" the existing controller, you would provide a new one, with a different route.

I'm a little confused about what you are calling a"custom content type". Do you mean one that you created yourself in code or from the admin UI?

Editor
Jun 2, 2011 at 10:13 PM
One I created from the Admin UI. I was hoping I could create a controller filer override similar to a view file override.

On Thu, Jun 2, 2011 at 6:09 PM, bertrandleroy <notifications@codeplex.com> wrote:

From: bertrandleroy

You can do anything. A theme is probably not the best place to put that sort of code but if you must, it's possible if you put a csproj in your theme. Note that you wouldn't "override" the existing controller, you would provide a new one, with a different route.

I'm a little confused about what you are calling a"custom content type". Do you mean one that you created yourself in code or from the admin UI?

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




--
Thanks,

Arra

Coordinator
Jun 2, 2011 at 10:18 PM

You *could* probably replace the controller but I don't see a single reason why you would want to. Just have a separate controller and have a different route point to it, or an equivalent route but with a higher priority, take your pick. From this controller, you can just query the ContentManager for any content, custom or not. There are plenty of examples in the application.

I would not attempt a driver in this case, but a handler should be able to intercept at the right place.

Still, I feel like I'm shooting in the dark here because you have not explained exactly what you were trying to do. What are you displaying, where, under what circumstances? In other words, what is your scenario?

Jun 3, 2011 at 10:06 AM
arock3 wrote:
I want to have a "Month" navigational structure that the user can toggle and based on the querystring I will only display News Items from that month. If I can access the List Rendering portion then I can add that nav structure above the list and also only render children who fall in those months.

Since you're talking about date-based navigation you could maybe look at the RoutableByDate component written by smeyers, discussed here: http://orchard.codeplex.com/discussions/255829 - it creates a URL structure like year/month/day, haven't tried it yet so I don't know what sort of UI it includes but maybe it'll give you a good start.

Editor
Jun 3, 2011 at 12:25 PM

@Bertrand, basically the site will have a page for news items. What I did was create a new content type from the UI called "NewsItem". This has fields of Body, and Parts of Date and Title. I then created a new list of "NewsItem" from the admin UI. When I browse to my news page it shows the list of news items I have added into the CMS. The final piece of functionality is to have some navigation at the top of this page that lets you filter News Items by month. I handled this so far by adding in a HTML Widget in the "BeforeContent" zone that contains links for each month of the year for the news layer. Then in the Summary template for each NewsItem, I check the month of the Date part and if it does not match the month in the querystring selected, I just don't render content for that item. This works, but as you stated it is not ideal and should be done in a controller, rather than at render time which I agree.

I was hoping I could just drop in a file to my Themes directory under like /Services or /Controllers that would override the core list rendering controller so it actually filtered out the items before getting to render time. This way I could also render the month navigation right above the list and only show links for months that had news in them, etc.

So that's my scenario. Let me know if you got any good recommendations. Once again thanks for the great responses.

Editor
Jun 3, 2011 at 1:13 PM

Also when using Shape Tracing I noticed I could just insert this template in the Views folder: List-url-about-us-news.cshtml and this would override the list rendering right?

Editor
Jun 3, 2011 at 6:09 PM

I am thinking I should create a content part that I can add to my list type that filters items by date. http://www.orchardproject.net/docs/Writing-a-content-part.ashx

 

Everyone agree? Anything easier would be great to know.

Jun 3, 2011 at 8:01 PM

Have you looked at the RoutableByDate part that I linked to? It already achieves this basic kind of thing and maybe you could base yours on that.

Coordinator
Jun 3, 2011 at 8:13 PM
Edited Jun 3, 2011 at 8:14 PM

I think your scenario needs Taxonomies rather than the built-in List feature. It will be a lot more flexible. For the date part, yes, makes sense.