Custom Part not rendering in Widget

Topics: Customizing Orchard, Troubleshooting, Writing modules
Jun 23 at 7:07 AM
Edited Jun 23 at 7:07 AM
crosspost: http://stackoverflow.com/questions/24359329/custom-part-not-rendering-in-widget-in-orchard-cms

I'm using Orchard 1.8, and I've built a part that I've attached to a widget. It's a basic part but I'm having issues rendering the template. Here's my code:

MODEL
public class AppsWidgetRecord : ContentPartRecord {        
}

public class AppsWidgetPart : ContentPart<AppsWidgetRecord> {    
}
DRIVER
public class AppsWidgetDriver : ContentPartDriver<AppsWidgetPart>
{
    protected override DriverResult Display(AppsWidgetPart part, string displayType, dynamic shapeHelper)
    {
        return ContentShape("Parts_AppsWidget",
            () => shapeHelper.Partial(
                TemplateName: "Parts/AppsWidget"
        ));
    }
}
I have an AppsWidget.cshtml under View/Parts

MIGRATION
        #region Define Apps Widget

        ContentDefinitionManager.AlterPartDefinition(
            typeof(AppsWidgetPart).Name, cfg => cfg.Attachable());

        ContentDefinitionManager.AlterTypeDefinition("AppsWidget", cfg => cfg
            .WithPart("WidgetPart")
            .WithPart(typeof(AppsWidgetPart).Name)
            .WithPart("CommonPart")
            .WithSetting("Stereotype", "Widget"));

        #endregion

        #region Create Apps Widget

        var appsWidget = _widgetsService.CreateWidget(homepageLayer.Id, "AppsWidget", "Apps", "5", "AfterContent");
        appsWidget.RenderTitle = true;
        appsWidget.Name = "apps";
        _contentManager.Publish(appsWidget.ContentItem);

        #endregion
PLACEMENT
<Place Parts_AppsWidget="AfterContent:5" />
The widget renders fine, the template of the part however, does not render at all. When I shape trace, the appswidgetpart is nowhere to be seen under the widget, but when I check the model it is there.

Image

I've also debugged and attached to the display function within the driver, and no issues come about. I don't see any errors in the logs as well.

Any ideas?
Jun 23 at 8:19 AM
There is a problem with the driver code. You shouldn't use Partial with the shape helper, but the name of the part. The name of the template file to be rendered is deduced by convention.

public class AppsWidgetDriver : ContentPartDriver<AppsWidgetPart>
{
    protected override DriverResult Display(AppsWidgetPart part, string displayType, dynamic shapeHelper)
    {
        return ContentShape("Parts_AppsWidget",
            () => shapeHelper.Parts_AppsWidget(
                // You can put here data you want to be available in your dynamic @Model object of your view
                Something: "some stuff"
)); } }



Jun 23 at 8:33 AM
Appreciate the prompt reply @kassobasi! :)

However, the part still doesn't render after changing the Driver Display function to:
return ContentShape("Parts_AppsWidget",
                () => shapeHelper.Parts_AppWidget());
The template for the part relative to the module is: Views\Parts\AppsWidget.cshtml
Jun 23 at 8:44 AM
Try
<Place Parts_AppsWidget="Content:5" />
in the placement file. See, the zone names in placement.info refer to local zones (Header, Content, Footer and Metadata, and maybe others, see Content.cshtml), not the zones in the theme. So the widget (the whole thing with the CommonPart and WidgetPart) will be rendered in the AfterContent as you created it in the migration, and the AppsWidget part will get the placement order according to the placement.info


Jun 23 at 3:14 PM
@kassobasi

Wow, you are totally right. Never knew that the placement of parts were relative to the Content Item. Thanks a lot! That solved it I think.