Developer forum

Forum » Templates » ItemPublisher with itemrelations Loop if/else issue

ItemPublisher with itemrelations Loop if/else issue

Roop K. Rusbjerg
Reply

Hello,

I'm stuck at something thats supposed to be simple but I just can't get it right.

What this peace of code to do, is to check if gallery loop is empty and it is then show the img and if it is not empty then show the gallery loop.

I have tried amny versions of the following but nothing seems to work:

  <div class="col-sm-12 imglist">

    @foreach (var i in GetLoop("ItemPublisher:Item.AddSimpleGalleryImgs"))  {

       if (GetValue("ItemPublisher:Item.AddSimpleGalleryImgs") != "null") {

         <a href='@i.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' data-fancybox="images">
           <img src='@i.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' class="img-fluid" width="24.7%" style="margin-bottom:3px" />
         </a>

  } else  if (GetValue("ItemPublisher:Item.AddSimpleGalleryImgs") == "nul") {
        <img src='@GetValue("ItemPublisher:Item.SimpleGalleriListImg")' class="img-fluid" />
     } 
  }
</div>
 
I hope you guys have a better solution for this.
 
Thanks!
 
Best wishes,
Roop 

Replies

 
Carsten Boll Dynamicweb Employee
Carsten Boll
Reply

Hej Roop :)

Long time no see!

I think the issue is that you check for null - I expect the tag to return an empty string instead of null so try using string.IsNullOrEmpty(String) instead.

/Carsten

 
Roop K. Rusbjerg
Reply

Hey Carsten,

Yup, been a while :)

I'm pretty sure that I have tried IsNullOrEmpty, like this - if (!string.IsNullOrEmpty("ItemPublisher:Item.AddSimpleGalleryImgs")) - but it dosn't seem to work. 

Is there another way to use it?

Best wishes,

Roop

 
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

You are now checking if the string "ItemPublisher:Item.AddSimpleGalleryImgs" is null or empty which it isn't. You should check if the value represented by "ItemPublisher:Item.AddSimpleGalleryImgs" is null or empty:

if (!string.IsNullOrWhiteSpace(GetValue("ItemPublisher:Item.AddSimpleGalleryImgs"))) {

Hope this helps,

Imar

 
Roop K. Rusbjerg
Reply

Hello Imar,

The first part of the code works, but the rest doesn't.

It should show gallery images when they're there and if not then it should show a single image of my choice.

So the last part where it should show a single image, that doesn't work.

Any ideas?

Best wishes,

Roop

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Isn't that a matter of an else block without further checks?

 else   {
        <img src='@GetValue("ItemPublisher:Item.SimpleGalleriListImg")' class="img-fluid" />
     } 
 
If not, can you post your current code?
 
Roop K. Rusbjerg
Reply

I have an else block but it's not doing what it's supposed to do :(

This is what it looks like now:

  <div class="col-sm-12 imglist">
    @foreach (var i in GetLoop("ItemPublisher:Item.AddSimpleGalleryImgs"))  {
    
    if (!string.IsNullOrWhiteSpace("ItemPublisher:Item.AddSimpleGalleryImgs")) {
    
      <a href='@i.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' data-fancybox="images">
      <img src='@i.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' class="img-fluid" width="24.7%" style="margin-bottom:3px" />
      </a>

  } else {  

      <img src='@GetValue("ItemPublisher:Item.SimpleGalleriListImg")' class="img-fluid" />

      }
  }
  
  </div>
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Maybe ItemPublisher:Item.SimpleGalleriListImg contains no value? Should it be ItemPublisher:Item.SimpleGalleryListImg?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Just saw this:

 @foreach (var i in GetLoop("ItemPublisher:Item.AddSimpleGalleryImgs"))  {
    
    if (!string.IsNullOrWhiteSpace("ItemPublisher:Item.AddSimpleGalleryImgs")) {
    

I think that if check doesn't make sense. You're already looping over that same tag, so I am pretty sure that !IsNullOrWhiteSpace is always true. Did you mean to test a different value?

 

 
Roop K. Rusbjerg
Reply

The system name of the item is correct, I've double checked all the system names.

And the ItemPublisher:Item.SimpleGalleriListImg always has a value.

It's kind of like a news/blog view with a list view image and when you click on one article you are directed to a detail view with some text and a gallery.

But if there is no gallery, then it should just show the list view image.

It should be so simple, but I can't get it to work.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

How's this:

@{
  var items = GetLoop("ItemPublisher:Item.AddSimpleGalleryImgs");
}

@if(items.Any()) {  // loop contains items
 foreach (var item in items)  {
         <a href='@item.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' data-fancybox="images">
           <img src='@item.GetValue("ItemPublisher:Item.AddSimpleGalleryImgs.AddSimpleGalleryImg")' class="img-fluid" width="24.7%" style="margin-bottom:3px" />
         </a>
  }
} 
else {
  <img src='@GetValue("ItemPublisher:Item.SimpleGalleriListImg")' class="img-fluid" />
}

This stores the loop in a variable called items. The Any() call then checks if there are any items. If there are, it loops over them and gets their value using item.GetValue. If the loop is empty, Any() returns falls and the SimpleGalleriListImg is shown.

Votes for this answer: 1
 
Roop K. Rusbjerg
Reply

AWESOME!

That worked!  :D

Thank you for the answer and the explaination.

Best wishes,

Roop

 

You must be logged in to post in the forum