Alternates for Parts

Topics: Customizing Orchard, Writing modules, Writing themes
Oct 12, 2011 at 9:19 PM

I have built a widget and I need to place two of them on the same page.

However, I also need to create an alternate for a part of that widget in both locations.

Normally if I just had one widget on the page I would just create an alternate for the part in the theme using the url of the page, like Part.XXX-url-homepage.cshtml.

But since I have two of them on the same page this doesn't work.

How can I specify an alternate for each part by using a zone, id, or something more specific?

Thanks,

Dave

Coordinator
Oct 12, 2011 at 9:48 PM

The most straighrforward way is maybe to do something like Content-id.cshtml

Oct 12, 2011 at 11:02 PM

That doesn't seem to work.

My only option for overriding a part's shape in a theme is just by URL?

There is nothing like Parts.XXX-id.cshtml or Parts.XXX-zone-Featured.cshtml?

I have 2 of these widget parts on a page and each part needs to be a bit different.

Coordinator
Oct 12, 2011 at 11:23 PM

Then enable the WidgetAlternates feature which has been added in 1.3, from an external contributor. Here is some comments from the source code:

// Add 2 alternates for flexible widget shape naming:
// [ShapeName]-[ZoneName].cshtml: (e.g. "Parts.Blogs.RecentBlogPosts-myZoneName.cshtml")
// [ContentTypeName]-[ZoneName].cshtml: (e.g. "RecentBlogPosts-myZoneName.cshtml")

And you can even use it in conjunction with other alternates like url- if you want

Oct 13, 2011 at 12:19 AM

As crazy as I find it to believe, I can't get any of those options to work in my Orchard Website as well as a default one I created from scratch.

The only way that I can seem to override my part in my theme or The Theme Machine is by URL only.

The Widget Alternates in 1.3 is doing nothing.

My part is Parts.XXX.cshtml

When I do:

Parts.XXX-Featured.cshtml or

XXX-Featured.cshtml

I get nothing.

If I do Parts.XXX-url-homepage.cshtml I get success. But that is worthless because I have two parts on the page and they need to be different.

Something has to be wrong here, but I don't know what. Too many hours in Orchard Code I think.

Coordinator
Oct 13, 2011 at 12:21 AM

Let me try it

Coordinator
Oct 13, 2011 at 12:39 AM

I found the issue. This feature is expecting too much from the shapes, and it makes it not to work in common cases. Here is how to fix it. It's a patch right now which will be part of next release now I have found this bug.
In the file Orchard.DesignerTools.Services.WidgetAlternateFactory, edit the code as this:

                if (displayedContext.ShapeMetadata.Type != "Widget") {
                    // look for ContentItem property
                    ContentItem contentItem = displayedContext.Shape.ContentItem;
                    
                    // if not, check for ContentPart 
                    if(contentItem == null) {
                        ContentPart contentPart = displayedContext.Shape.ContentPart;
                        if(contentPart != null) {
                            contentItem = contentPart.ContentItem;
                        }
                    }

                    if (contentItem != null) {

Oct 13, 2011 at 4:02 AM

Cool. The patch works, but it's not providing alternatives to Part.XXX.

XXX-Featured.cshtml doesn't get the Model I give to Part.XXX.cshtml in its Driver.

Parts.XXX-url-homepage.cshtml is perfect. This is what I am striving for, but I just need more alternatives like Parts.XXX-Featured.cshtml or Parts.XXX-23.cshtml to work.

I'll just write another widget as it would be faster, but I am surprised this hasn't come up before. If it was thought we might want to override a part's shape by URL, you would think it would be useful to override them at more finer levels for better control.

Although this is cool. I am not sure how it helps my problem.

Coordinator
Oct 13, 2011 at 6:04 AM

What is XXX ?

By the way as you get WHATEVERPART-id, it should work wherever it is.

Nov 18, 2011 at 4:59 PM

Is there an open issue for this? I'm using Sebastien's patch above. The url alternates are working fine and you can alternate "widget" by zone as in "Widget-AsideLeft.cshtml", but that's applied to any widget I place in that zone. Zone alternates for specific content parts are not available as the comments in Orchard.DesignerTools.Services.WidgetAltnernates suggests:

 

// Add 2 alternates for flexible widget shape naming:
// [ShapeName]-[ZoneName].cshtml: (e.g. "Parts.Blogs.RecentBlogPosts-myZoneName.cshtml")
// [ContentTypeName]-[ZoneName].cshtml: (e.g. "RecentBlogPosts-myZoneName.cshtml")

 

For example, I'm using the BetterMenus module to handle my main menu. I'm overriding "Parts.BetterMenu.cshtml" and "Parts.BetterMenuItem.cshtml" this in my theme. My approach to an aside navigation was to activate the WidgetAlternates feature and add an alternate like "Parts.BetterMenu-AsideLeft.cshtml" (AsideLeft is a zone); the alternate is not found.

Thank you,

Matthew