Developer forum

Forum » Templates » Foreach loop rendering empty fields in ItemPublisher module

Foreach loop rendering empty fields in ItemPublisher module

Aditya Bante
Reply

Hi I am using foreach loop to render fields in Item publisher module. It renders the fields data but along with it it also renders a lot of empty fields as you can see it in a attached PNG. 

I am pretty sure there is something very wrong with my loop construction, and I am not sure what that is here is the loop

<div class="items">
@if (@GetBoolean("ItemPublisher:Items.Any") == true)

    <ul class="items-list" style="text-align:center; color:white">     
        @foreach (LoopItem i in GetLoop("ItemPublisher:Items.List")){
        <li class="items-list-row items-clear">                                  
            @foreach (LoopItem field in i.GetLoop("ItemPublisher:Item.Fields"))
            {         
                <span class="event-place">
                    @if (!string.IsNullOrEmpty(@field.GetString("ItemPublisher:Item.EPlace")))
                    {
                        @field.GetString("ItemPublisher:Item.EPlace")
                    }
                </span>
                 <span class="event-date">
                    @if (!string.IsNullOrEmpty(@field.GetString("ItemPublisher:Item.EDate")))
                    {
                        @field.GetDate("ItemPublisher:Item.EDate").ToString("dd.") @field.GetDate("ItemPublisher:Item.EDate").ToString("MMM")
                    }
                </span>
                <span class="event-time">
                    @if (!string.IsNullOrEmpty(@field.GetString("ItemPublisher:Item.ETime")))
                    {
                        @field.GetString("ItemPublisher:Item.ETime")
                    }
                </span>         
            }

        </li>
        }
    </ul>
}
</div>

any suggestions? thanks

Best Regards
Aditya Bante

 

Frontendoutput.png

Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Your approach is wrong.

You have items in a list and you loop them - just access the fields inside that loop and not inside the loop of fields.

You loop fields inside the list loop - that is wrong, since fields are in the root at the same level as your list loop.... So basically just remove your fields loop and leave the rest of the code...

Votes for this answer: 2
 
Aditya Bante
Reply

Thanks Nicolai :) It works.

 
Jens Mouritzen
Jens Mouritzen
Reply

I have kind of the same issue. When i wrap my loops in a <li></li> the code breaks. I have attached some images showing the code in the documents and the errorcode it outputs.

CalendarLoopList.png LoopList.png
 
Nicolai Pedersen
Reply

Hi Jens

I do not see the error? Could you please ellaborate?

Thanks, Nicolai

 
Jens Mouritzen
Jens Mouritzen
Reply

Sorry, attached a txt file

 
Nicolai Pedersen
Reply

Hi Jens

You get an Exception saying "Line 178: The name 'dates' does not exist in the current context". So it seems like you have a syntax issue in there somewhere.

Check this out and see if that can help you: https://www.google.dk/?gws_rd=ssl#q=the+name+does+not+exist+in+the+current+context+razor&*

First link is to a stackoverflow that could give some pointers. Otherwise report back, and we will see if we can figure it out.

BR Nicolai

 
Jens Mouritzen
Jens Mouritzen
Reply

Thanks. I found the error. I was missing the @ in the other "foreach".
Razor novice here :-)

 
Nicolai Pedersen
Reply

Great - no sweat, I also did not see the error :-)

 
Jens Mouritzen
Jens Mouritzen
Reply

Hi guys.

I colud not post a new thread so i'm writing in this one.
I'm very new to Razor templates. I got a event booking site that needs an event calendar sorted by month. The events are made with items and display with the itempublisher (it's a DW9 solution). The hierarchy/loop would look like this (see image 1).

Right now it loops the items themselve, but it needs to loop the months from the date field in the item and iterate the items inside that loop. (see code on image 2)

The calendar can also be seen on http://csb.dw9.dynamicweb-cms.com/calendar

image1.png image2.png

 

You must be logged in to post in the forum