Custom View For RecentBlogPosts in Orchard

和自甴很熟 提交于 2019-12-08 01:39:23

问题


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.



来源:https://stackoverflow.com/questions/5514499/custom-view-for-recentblogposts-in-orchard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!