Iterate over items in projection list

Topics: Writing modules, Writing themes
Sep 10, 2012 at 11:30 AM

How can I create an alternate for items in Projection? I have created an alternate calles List-HomePageFeatured.cshtml which I am able to debug. Problem is that when I check Model.List it is empty.

How can I iterate over the items in the query/projection?

Sep 10, 2012 at 6:49 PM

In Orchard 1.4.2 I was able to create an alternate "Parts.ProjectionPart-url-homepage.cshtml"

and iterate over the items like this

 @foreach (var item in Model.List)    {



It´s not possible to override Parts.ProjectionPart-<xxx> in 1.5.1?

Sep 12, 2012 at 5:12 PM

Nothing should have changed in 1.5, but this is not the right way to do this. Why don't you just use a projection layout in property mode?

Sep 12, 2012 at 8:42 PM

Ok you mean I create my own custom layout by overriding Layoushapes.cs, GridLayout.cs, GridLayoutForms.cs?

What do you mean by "property mode"? Display Mode for the query layout to property instead of content?

Sep 12, 2012 at 8:45 PM

No, you should be able to do that with the built-in layouts. Add a layout to your query, and edit it. What I mean by "property mode" will become instantly clear.

Sep 12, 2012 at 8:48 PM

I get what you mean but the built in layouts only render tables and grids. What if I want some control over the rendered content like extra divs and stuff? Don´t I have to greate my own custom layout by implementingn logic?

Sep 12, 2012 at 9:45 PM

no, they don't. There is one for tables and one for ul/lis, that you can customize to emit any tag you want. Rewrite is what you're looking for. It's right in front of you ;)

You can also create your own layout but you don't need to.

Oct 5, 2012 at 2:42 PM

I'm having the same issue having just updated to 1.5.1.

I had overridden Parts.ProjectionPart-<xxx> in 1.4.2 to include a random number generator to switch styles.

    IEnumerable<object> featuredItems = Model.List;
    Random randomizer = new Random();
    string[] colours = new string[5];
    string[] markers = new string[4];
@if (featuredItems == null || featuredItems.Count() < 1) {
    <p>@T("No posts.")</p>
string title = post.Title;
 ContentItem item = post.ContentItem;
@foreach (dynamic post in Model.List) {   
<li class="@markers[randomizer.Next(4)] @colours[randomizer.Next(5)]"> <h6>@Html.ItemDisplayLink(title, item)</h6> <a href='@Url.ItemDisplayUrl(item)'> <p>@post.Fields.CaptionColour.Value</p> </a> </li> }

However the Parts.ProjectionPart-<xxx>.cshtml file no longer gets called.

I had tried to do the same thing in Widget.Projectionwidget but I can't find the list of posts to iterate over.

Is there a way of doing this or something similar in 1.5.1?

Oct 10, 2012 at 1:27 AM

I don't think this should have changed between 1.4 and 1.5. This should still work I think. Are you sure nothing changed on your end?

Oct 10, 2012 at 8:28 AM

The only change made other than the upgrade was to remove the Advanced menu module which was incompatible and replace it with the new in-built menu.

This was separate from the Projection List and shouldn't have effected it.

Oct 10, 2012 at 4:27 PM

Then that looks like a regression. Please file a bug with repro steps.