Phill Healey
Phill Healey

Reputation: 3180

Orchard CMS Recent Blog Post Widget Alternate View

I'm new to Orchard CMS and MVC so please forgive my ignorance. I've done a search and not found anything apparently relevant....

In one of the Footer Quad zone on my Orchard site I want to show the 5 most recent blog posts, just by title. At present it show the title, post, tags etc all in their unformatted state(Ive not style the blog yet).

Using the tracing tool I've created an alternate view called;

Parts.Blogs.RecentBlogPosts

However the only content in this view is;

@Display(Model.ContentItems)

Which -unlike other installed widgets Ive edited alternate views for- doesn't give me anything to play around with to create the layout I'm needing to create.

Have I selected the wrong shape / view, or do I need to get stuck in with some coding???

As I say, I'm new to both MVC and Orchard but very keen to learn. Any help on this will, as always, be greatly appreciated.

Upvotes: 2

Views: 2923

Answers (3)

Bartosz
Bartosz

Reputation: 4592

I got the exact same problem while trying to differentiate recent blog posts layout from blog summary posts. I followed this concept

http://weblogs.asp.net/bleroy/archive/2011/07/31/so-you-don-t-want-to-use-placement-info.aspx

and created Parts.Blogs.RecentBlogPosts alternate. Then while navigating through the model using shape tracing tool I found all elements I was looking for and put them in my new shape. I know that my approach might not be the most appropriate one but it works. Maybe it would be a starting point for someone who would need to do similar thing:

<div class="last-news">
<ul>
@foreach (var item in Model.ContentItems.ContentItems.Items)
{
    var max = (item.ContentItem.BodyPart.Text.Length > 100) ? 100 : item.ContentItem.BodyPart.Text.Length;

    <li><header>@Display(item.ContentItem.CommonPart.PublishedUtc.ToShortDateString())</header></li>
    <li><h1><a href="/@item.ContentItem.Parts[5].Path">@Display(item.ContentItem.TitlePart.Title)</a></h1></li>
    <li>@Display(Html.Raw(item.ContentItem.BodyPart.Text.Substring(0, max)))</li>
}
</ul>
</div>

Upvotes: 3

Bertrand Le Roy
Bertrand Le Roy

Reputation: 17814

It does deserve an explanation. Model.ContentItems here really is a List shape. When you call Display with it, it will locate the most appropriate template for a list shape (and that can be an alternate) and renders it. The default List rendering just renders UL/LI and in each LI calls Display on the individual Content shape for the list element, but with the Summary display type. When that in turn gets rendered, the system locates the most relevant Content template, which usually renders a bunch of zones, inside of which the parts get pushed according to placement. So there is a lot going on, in quite a few nested levels. Still, it is possible to override the whole thing at any level. The difficulty is to know what to put there. The easiest for this is to explore the Model in shape tracing and see what you can use. This article shows how to override list rendering in a particular situation and take it over entirely: http://weblogs.asp.net/bleroy/archive/2011/03/27/taking-over-list-rendering-in-orchard.aspx

Upvotes: 2

Artyom
Artyom

Reputation: 3571

The Parts.Blogs.RecentBlogPosts view displays Parts_Blogs_BlogPost_List shape (Parts.Blogs.BlogPost.List.cshtml) which displays BlogPost content items. (You can see it in RecentBlogPostsPartDriver.cs). BlogPost content type consists of TitlePart and BodyPart parts which has their own shapes and views (or templates in Orchard terminology). So to make some corrections for those templates you could try to alternate Parts_Blogs_BlogPost_List, Parts_Common_Body_Summary and other shapes.

Please see the following instructions on Orchard docs page especially:

  1. Alternates
  2. Accessing and Rendering Shapes

Upvotes: 1

Related Questions