Creating a new recent blog post widget

Dec 28, 2010 at 6:20 PM

The recent blog post widget formats things in a strange way, by replacing all list items with a paragraph. So I wanted to create a new widget that just displays the title of each item. Where is this formatting of the content list occurring? And what are the parts needed to create a new widget? It's a bit confusing looking at the Blog Module determining how this fits together.

Thanks in advance,

Coordinator
Dec 29, 2010 at 6:11 AM

No need to create a new widget. You just need to override the template in your theme. Parts.Blogs.RecentBlogPosts.cshtml seems like a good place to start.

Dec 29, 2010 at 8:32 PM

So it looks like the formatting is controlled by the Parts.Common.Body.Summary.cshtml which is the generic "content summary" display template. 

When I override the Parts.Blogs.RecentBlogPosts.cshtml, I am trying to break 

 

@Display(Model.ContentItems)

Into its individual components. Wouldn't that look something like below? 

<div style="border:solid 1px red;">
  @foreach(var item in Model.ContentItems) {
   <p>@Html.ItemDisplayLink(T(item.Name.ToString()).ToString(), item)</p>
  }
</div>

Thanks for your patience, I'm new to both Orchard and Razor.

Sincerely,


 

 

Coordinator
Dec 29, 2010 at 9:58 PM

Yes, that looks about right.

Jan 6, 2011 at 4:31 AM

Unfortunately, the ItemDisplayLink doesn't seem to be available in the Theme project. Also, if I get rid of it and just try and display item.Name.ToString() without the link nothing shows up. All I get is the red outline.

Does anyone have working example of overriding the Parts.Blogs.RecentBlogPosts.cshtml to get better control over how the content is displayed?

Thanks in advance,

Coordinator
Jan 6, 2011 at 2:50 PM

ItemDisplayLink is an extension method. Maybe you need to add a using for Orchard.Mvc.Html, though it is added by defaut in the web.config.


May 4, 2011 at 10:23 AM
ItemDisplayLink is extension method and passed "var item in Model.ContentItems" is not IContentItem so there is no such signature for ItemDisplayLink.

I've also wanted to have last 5 titles of blog posts only. Here is my solution in my custom theme.

Parts.Blogs.RecentBlogPosts.cshtml

@using Orchard.ContentManagement;
@using Orchard.Utility.Extensions;
@using Orchard.Blogs.Models;
@using Orchard.Core.Routable.Models;
@{
    var count = Model.ContentItems.ContentItems.Items.Count;
}

@Html.UnorderedList(
  (IEnumerable<dynamic>)Model.ContentItems.ContentItems.Items,
  (t, i) =>
    {
      return MvcHtmlString.Create(
        string.Format(
@"    <article class=""content-item blog-post"">
      <header>
        <h1>{0}</h1>
      </header>
    </article>",
               @Html.ItemDisplayLink(((ContentItem)t.ContentItem).Get<RoutePart>()).ToHtmlString()));
    },
    "blog-posts content-items")



Coordinator
May 4, 2011 at 6:21 PM

The String.Format part is quite ugly seeing how easy it is to do the same thing with the plain Razor syntax. See http://weblogs.asp.net/bleroy/archive/2011/03/27/taking-over-list-rendering-in-orchard.aspx