Overriding List rendering

Topics: Troubleshooting, Writing themes
May 11, 2011 at 4:49 PM

Hi.

I'm investigatign the posibility of moving our corporate site to orchard and have stucked with a simple problem.

I have:

- orchard v. 1.1.30; some modules installed (Commerce Sample, WebStore e-commerce) for learning purpose;

- 1 ContentPart (ProductPart); 1 ContentType Product which includes ProductPart and also Containable and Common Parts (so it can be in a list); 1 List of products, say Catalog.

I need to place a list of products (ContentParts) in a two column way.

I've browsing for a while and some of discussions says that you can change List rendering by putting List.cshtml file into your themes folder (like http://orchard.codeplex.com/discussions/246555 or http://orchard.codeplex.com/discussions/251374 or http://orchard.codeplex.com/discussions/234307)

But all I have, if I put List.cshtml into ~/Themes/myTheme/ folder, is

"An item with the same key has already been added." exception

[ArgumentException: An item with the same key has already been added.]   System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9382923   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +253   Orchard.DisplayManagement.Descriptors.<>c__DisplayClassb.<GetShapeTable>b__2(AcquireContext`1 x) +1660   Orchard.Caching.Cache`2.CreateEntry(TKey k, Func`2 acquire) +371   Orchard.Caching.<>c__DisplayClass3.<Get>b__1(TKey k, CacheEntry currentEntry) +279   System.Collections.Concurrent.ConcurrentDictionary`2.AddOrUpdate(TKey key, Func`2 addValueFactory, Func`3 updateValueFactory) +81   Orchard.Caching.Cache`2.Get(TKey key, Func`2 acquire) +344   Orchard.Caching.DefaultCacheManager.Get(TKey key, Func`2 acquire) +143   Orchard.DisplayManagement.Descriptors.DefaultShapeTableManager.GetShapeTable(String themeName) +206

etc.

Am I the only one who faced this problem?

May 11, 2011 at 5:03 PM

I've seen that error when two shapes with the same name existed.

If you want an alternative to the Lists system, you could try my Mechanics module. It will also allow products to be in multiple categories (which is a limitation of Lists).

There's a simple example of building product/category relationships in the documentation: http://scienceproject.codeplex.com/wikipage?title=Mechanics

You can create a Socket.CategoryToProduct.cshtml file to override the template when products are displayed in categories.

May 12, 2011 at 12:02 PM

Thank's randompete. I've read your Mechanics module documentation example and that's exactly what I need. Will examine your code closer today.

BTW, do you have any running site with your modules on it, or this project is in work so no publicly available examples yet?

May 12, 2011 at 12:14 PM

No public sites as yet, although there are two sites I'm working on where I'm using it, these have been proof-of-concept and the basis for my feature requirements (although I've also started building "Theory" projects which are available in the source on codeplex, that demonstrate how to build common scenarios like navigation or product/category websites - see Cartography and Economics modules).

I'll add it to my Downplay website as well towards the end of today. Additionally I started recording a screencast to demo some features but it's quite time consuming and not that easy! But I can help out pretty quickly with any specific questions or integration problems you might have.

May 12, 2011 at 9:22 PM

Back to my first problem. (just don't like when things happening and no explanation found)

Windows XP, VS 2010; .NET Framework v4.0.30319; MVC 3; Orchard.Source.1.1.30;

New site, new db on sql server 2005. After installing all I do is copy List.cshtml to folder ~\Orchard\src\Orchard.Web\Themes\TheThemeMachine\Views which contains only "@Display(Model.ContentItems)"

Result - "An item with the same key has already been added. " exception

Each day I'm closer to the idea that orchard is not ready for commercial usage.

Does anybody know any more or less complex site on orchard? (excluding sites like "My homepage" which is usually trivial)

Coordinator
May 12, 2011 at 9:36 PM

Are you trying to override all list rendering on the site or just the rendering of *a* list? It appears like you just need to have a list of products rendered differently. If so, is it the item rendering that you want to customize or the actual list?

May 13, 2011 at 2:08 AM

I think the error you're seeing has been fixed in this workitem: http://orchard.codeplex.com/workitem/17764

So you could update to the latest source. Bertrand, he says he needs to render the list in two columns. Actually it could be done crudely with CSS floats, without overriding any templates. But to actually divide the list properly into columns you perhaps need to do some more heavy shape customization anyway. You can use an IShapeTableProvider to hook into the list shape, check for specific content types or other properties, and then add an Alternate shape name for content types that you want to split; for instance List_TwoColumn, which could then be rendered by a List.TwoColumn.cshtml template.

May 13, 2011 at 6:46 AM

Sorry for my scream for help (previous post). I was talking in general. I see something that cannot be reproduced and that's annoying me. More of that, While I was testing my module I saw non deterministic behaviour. Will try to explain. I've made a simple module step by step using this http://orchardproject.net/docs/Writing-a-content-part.ashx manual. Everithing works fine. Then I removed my database  and started with new, empty one. Somehow fields from my content part didn't appeared. And I can't create any instance of content type (content item) while it doesn't have Common part. It seems for me that this product (orchard) is far from release. Sorry for off-topic. Will try the latest source. And thank's to randompete for your ideas. Your information is valuable.

Coordinator
May 13, 2011 at 6:51 AM

I'm not sure I understand the problems you're describing as it is, but if you find bugs, you should report them in the issue tracker.

May 13, 2011 at 9:50 AM

You need CommonPart. Everything has to have to it. That's why it's called CommonPart ;)

As for fields disappearing, were they custom fields you added in the UI? If you want them to reappear in a new database you should add them in a Migrations file instead, then you can recreate them every time.

If not, maybe you could detail a bit more, I'm sure there's a good reason.

May 13, 2011 at 5:34 PM

I've found the solution to one of my problems.

The history was - I created content part "product", then renamed it to "abcProduct" and complitely forgot to change my Placement.info file. So in ApplyImplementation method of ContentShapeResult class placement.Location was null and returnet from ApplyImplementation procedure on line 34. As a result I could not see the shape which magically stops to creating. My mistake I know, but it's better be an exception rather than silently behavior.

If I started about CommonPart. Can you show me where can I read more about ContentParts and ContentTypes? I feel like I don't know much about them. For example there is a content type (menu) which doesn't contain CommonPart. But you told me that "Everything has to have to it.". Some of content types have "List Items" and "Create New ContentTypeName" links and some of them don't. You have classes like "MenuItemPart" in Core project but no "Menu Item" part is available to use.

May 13, 2011 at 5:43 PM
Edited May 13, 2011 at 5:43 PM

Regarding the first point, it shouldn't be an exception. One reason being, if you render a ContentItem with an entirely new DisplayType, placement won't exist for most parts and you have to specify yourself which parts you want to appear. This is useful behaviour. If a part isn't displaying then it'll nearly always be either Placement, or problems in the driver. But there are times when you want to create shapes that don't necessarily have to have placement.

About CommonPart - yes there are examples where it isn't used. I was just stating the simple version which is true for the majority of cases. The thing about the Menu type is that it only gets used from code, not from the normal editing or display UI - which is why it doesn't need Common.

The place to start for more information about the content framework is Orchard's documentation: http://orchardproject.net/docs/  - it has a lot of information, examples and tutorials. Beyond that, Bertrand's blog is definitely worth a read to gain some deeper understanding, and if you have any more specific questions just ask. Most of what I've learned was by digging thru source code, looking at other people's modules, and just plain experimenting.