Loudenvier
Loudenvier

Reputation: 8814

How to access an item parts when custom rendering a list in Orchard

I've taken over the rendering of a List in Orchard to be able to create custom markup (I need to create an interactive navigation before the actual contents of the list). This is the code I'm currently using:

@using Orchard.DisplayManagement.Shapes;

@{
    var list = Model.List;
    var items = list.Items;
    var count = items.Count;
    var listTag = Tag(list, "ul");
    listTag.AddCssClass("historia");
    var index = 0;
}

<div class="produtos">
    @foreach (var item in items)
    {
        var itemTag = Tag(item, "div");
        itemTag.AddCssClass("item-" + index);
        @itemTag.StartElement;
        <h3>
        @item.Title
        </h3>
        <p>@item.MyCustomTextField</p>
        @itemTag.EndElement;
        ++index;
    }
</div>

@Display(Model.List)

@Display(Model.Pager)

It almost works... I'm not being able to access the list item fields... I want to grab the title and and put it inside a h3, then put a custom text field in a paragraph following it... I'll use CSS to render this navigation part correctly. After this navigation part I'll call the standard list rendering routine.

The problem is that @item.Title yields a null/empty string. I want to access the item contents (no pun intended) which includes a custom text field which is an excerpt for the entire post that will be shown on the "navigation pane".

Bertrand shows us how to do that with blog posts:

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

But it does not work the same way with standard Lists.

(Another little question: could/should I have used a custom wrapper instead of overriding the entire layout for this list?)

Upvotes: 4

Views: 2622

Answers (1)

Bertrand Le Roy
Bertrand Le Roy

Reputation: 17814

The blog example is very different. In the case of a list, what you have in Items is shapes, not content items. Now these shapes often (but not always) have a reference to the corresponding content item, usually under the ContentItem property. So to get the title for example, item.ContentItem.TitlePart.Title would give you what you want.

As for your wrapper question, I don't know what you're trying to do so I can't give advice. If you want to keep the list rendering intact but surround it with additional markup then yes, that's one way to do it.

Upvotes: 5

Related Questions