Custom View For RecentBlogPosts in Orchard

2019-05-23 16:05发布

问题:

I've been searching and trying for 2 days to change the view for the RecentBlogPosts content type that appears on my homepage. I want to display the title and a phrase from the posts.

I have managed to find a view that shows the title for each post, but I haven't managed to figure out what the other parts in the Model are named or how to extract the text.

Help will be GREATLY appreciated !

@using Orchard.ContentManagement;
@using Orchard.Core.Routable.Models;
@using Contrib.Hyperlink.Fields;
@{


  IEnumerable<object> blogPosts = Model.ContentItems.ContentItems;
}
@if (blogPosts == null || blogPosts.Count() < 1)
{
    <p>@T("No posts.")</p>
}
else
{
   <div class="content-items">
   @foreach (dynamic post in blogPosts)
   {
            string title = post.Title;
            ContentItem item = post.ContentItem;

   <div class="blogpost" style="width: 300px; padding-left: 15px; float: left;">
         <p class="content-item-summary">@Html.ItemDisplayLink(title, item)</p>
   </div>

   }
   </div>
}

The above code was (mainly) from Bertrand Le Roy's blog, for which I thank him.

回答1:

The ContentItem is a BlogPost content item, which contains BlogPostPart (from Orchard.Blogs.Models namespace). So you can use any properties it has - checkout the link to the source code I provided above.

This part has the Text property, which returns the Html from corresponding BodyPart (which BlogPost type has attached too) under the hood. And btw - Remember to use @Html.Raw(part.Text) to display the fetched content.

One more thing - for making life easier I'd rather cast that iterated objects to BlogPostPart to get Intellisense:

@foreach (var post in blogPosts.Select(p => ((IContent)p.ContentItem).As<BlogPostPart>())) 
{ 
    ... 
}

HTH



回答2:

You might want to try the shape tracing feature in Orchard 1.1 (due next week). The model tab in there will enable you to dig into the objects and figure out the syntax to display pretty much anything.