Recent posts widget customization

Jan 22, 2011 at 8:20 PM


I was wondering if it was possible to configure the recent posts widget summary length. I noticed that the default blog posts list only displays X number of characters, is it possible to control this summary limit? Also is is possible to include images in this summary?

Future enhancement would be a preview of the summary below where the post was being authored.

Many thanks,



Jan 23, 2011 at 5:42 AM

Seems like a good idea. Could be an interesting patch ;)

Jan 23, 2011 at 10:28 AM

Indeed. But in the short term I just need to extend the character limit, not done any orchard development yet. I'm assuming the summary will get set in the core rather than the view template. Is that right?

Jan 24, 2011 at 5:09 PM

I have the same requirement. I'll cook up something.

Jan 24, 2011 at 5:40 PM

OK, it's actually fairly simple. Add a file named Parts.Common.Body.Summary.cshtml into the Views directory of your theme, with this contents:

    Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
    string bodyHtml = Model.Html.ToString();
    var body = new HtmlString(Html.Excerpt(bodyHtml, 400).ToString().Replace(Environment.NewLine, "</p>" + Environment.NewLine + "<p>")); 
<p>@Html.ItemDisplayLink(T("Read more...").ToString(), contentItem)</p>
The red number here is what determines the length of the summary. Also, you could replace that with your own logic if you wanted to.

Feb 2, 2011 at 8:43 PM

Thanks Bertrand. It's starting to make sense but what I don't quite understand is how you worked out the file must be called Parts.Common.Body.Summary. I can't find any documentation about the common parts.

Also if I override this part to match my recent blog posts design, hasn't that then overridden other content types body summaries also. Is there a way to target just a specific module in this case the blog.

I may have totally missed where this is documented.



Feb 2, 2011 at 9:13 PM

You are absolutely right that documentation is somewhat lacking in that area, as is good tooling to make this more discoverable. I'll explain how I figured it out, and some of it will be a little technical but I hope I can keep it usable.

I know that the body of a content item is handled by the body part that is in the Core.Common module. I open the views directory of that volume and hunt for template files in there. So you determine what part is responsible for what you want to customize and try to find the right template in the views folder of that module.

Once you've done that, you can also look for alternates. You do that by looking for a class implementing IShapeTableProvider and see if anything is handling the shape you're looking for and adding alternates. You can see examples of that by searching the code for AddAlternates. If you don't find an alternate that already exists that would enable you to specialize the template the way you want. That's where the technique I exposed in post enters into play and enables you to add your own alternates. Between all those techniques (plus you should be able to customize pretty much anything in any way you can think of.

Does this help?

Feb 2, 2011 at 9:42 PM

Ok. Say I want to customize the metadata information displayed on a recent blog post item.

I know that the recent blog post item is displayed by the Content.Summary.cshtml which in turn calls

<div class="metadata">

I can see that in Shapes.cs there is an Alternate for blog posts.

if (contentItem != null) {
                        displaying.ShapeMetadata.Alternates.Add("Content__" + contentItem.ContentType);


So in order to target the meta data on a blog post only I tried creating a chtml template in my theme named:



Not working. What am I missing...


I'm sure this will all click soon.

Thanks for all your help.



Feb 2, 2011 at 11:42 PM

Uh. I was pretty sure I had answered this but it appears my answer got lost in the intertubes... Oh well.

Yeah, you have two shapes at play here: the content item's shape (that does have an alternate with the content type) and the metadata part that is provided by the Orchard.Common module. That one is called Parts_Common_MetaData and has no alternates that I know of. This is where the technique I showed in my post comes in handy as it enables you to add your own alternates, for example based on the content type name.

Makes sense?

Feb 2, 2011 at 11:43 PM

Oh, and the metadata shape is just one of the shapes that will get rendered inside of the more global content item shape. It's a hierarchy in case that wasn't clear.

Feb 3, 2011 at 7:50 PM


Still struggling with this. I've added the below but a breakpoint on the ContentItem doesn't get hit. I've found this shape mentioned by name in the src so I know its correct.

public class MetaDataShapeProvider : IShapeTableProvider
        private readonly IWorkContextAccessor workContextAccessor;

        public MetaDataShapeProvider(IWorkContextAccessor workContextAccessor)
            this.workContextAccessor = workContextAccessor;

        public void Discover(ShapeTableBuilder builder)
              .OnDisplaying(displaying =>
                  ContentItem contentItem = displaying.Shape.ContentItem;
                  if (contentItem != null)
                    displaying.ShapeMetadata.Alternates.Add("Metadata__" + contentItem.ContentType);

Any ideas?

If I'm understanding this concept correctly this should create an alternate with the name "Metadata_BlogPost that I can use to override the display of that part.

I'm working off the below post for anyone that's interested.


Many thanks,