New DisplayType for items for Parts.Blogs.RecentBlogPosts.cshtml?

Topics: Customizing Orchard, Writing themes
Jan 31, 2014 at 7:18 AM
I have a nice summary view for my blog, but want my recent blog posts to have a different, more compact view. I am trying to use BuildDisplay with the "Short" display type, and have my setup right, but am not sure the best way to modify Parts.Blogs.RecentBlogPosts.cshtml to make this work.

Some examples of customized Display types and rendering the same content beyond Edit, Admin, Summary, Detail would be helpful.

This works (partially) in my Parts.Blogs.RecentBlogPosts.cshtml:
@foreach (var item in Model.ContentItems.ContentItems.Items)
    @Display(BuildDisplay(item.ContentItem, "Short"))
The first issue I have with this is it seems to have too much knowledge of the Model, that I know the first level is a list, and that list has ContentItems which cannot be passed to BuildDisplay as they are not IContent it seems. Some objects with name ContentItem seem to work with BuildDisplay and other don't. Model.ContentItems.ContentItems.Items doesn't work either, but item.ContentItem does.

Second, I have a specialized Parts.Common.Metadata.Short.cshtml that I would like to be invoked, but it doesn't seem the display type is recursive.

Ideally, my template would be just this:
@Display(BuildDisplay(Model.ContentItems, "Short"))
Or, better yet I would just be able to select a display type when I add the widget to the layer in the widget config page. If I figure out how to do that at all, I might do that and contribute back.

BTW, the shape tracing tools have not been much help, the model is not complete and it is hard to see what shapes were actually involved in rendering this view. The is a nice idea and when the shape tracing works it is not so bad, but often I find myself fiddling with stuff a long time to make something show up on the screen. Especially when there is a hierarchy here... how do I tell the main shape Parts.Blogs.RecentBlogPosts that the List shape inside should render each shape inside with my "Short" display type. I am new to Orchard but not found much help on troubleshooting techniques on interplay between and templates, and how to trace when shape tracing doesn't do the trick. I know I might be missing something but this seems a bit too hard.
Jan 31, 2014 at 7:05 PM
In order to gain more control over the output of the Recent Blog Posts widget, I override the shape and iterate through the items instead of letting Orchard determine the layout
@using Orchard.ContentManagement;
@using Orchard.Blogs.Models;
@using Orchard.MediaLibrary.Fields
@using System.Linq;

    IEnumerable<object> blogPosts = Model.ContentItems.ContentItems;

@if (blogPosts == null || blogPosts.Count() < 1) {
    <p>@T("No posts.")</p>
else {
    <ul class="recent-posts">
        @foreach (dynamic post in blogPosts) {
            string title = post.Title;
            ContentItem contentItem = post.ContentItem;
            var part = (Orchard.ContentManagement.ContentPart)post.ContentItem.BlogPostPart;
            var imageField = part.Fields.Where(field => field.PartFieldDefinition.Name == "BlogPostImage").SingleOrDefault() as MediaLibraryPickerField;
            DateTime date = post.ContentItem.CommonPart.CreatedUtc;

                @if (imageField != null) {
                    foreach (var mediapart in imageField.MediaParts) {
                        <img src="@mediapart.MediaUrl" alt="@mediapart.AlternateText" class="img-responsive" />
                    @Html.ItemDisplayLink(title, contentItem)
                    <small>Posted @date.ToShortDateString()</small>
this code also checks for a MLPF attached the BlogPost Part for a thumbnail. Hope this helps in some small way to achieve what you're looking for.