Developer forum

Forum » Templates » Item publisher

Item publisher

Jens Mouritzen
Jens Mouritzen
Reply

I'm trying to make a template for the item publisher. The purpose is to display a link if a item relation list, in the items, has content. The attached image "template" is the code i got so far.
I got a short video showing the items with images in the item relation list (the ones that should have links to the items). The ones without should not link to the details page og the items.

https://www.screencast.com/t/cmgTMZfa

Sorry, i'm not very good describing such issues, i hope you guys understand anyway. I hope someone can help us. I've also pasted the template code below...

@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>

@foreach (var i in (GetLoop("ItemPublisher:Items.List"))){

  string title = i.GetString("ItemPublisher:Item.Title");
  string description = i.GetString("ItemPublisher:Item.Description");
  string link = i.GetString("ItemPublisher:Item.Link");
  string url = i.GetString("ItemPublisher:Item.Url");
  string backgroundcolor = i.GetString("ItemPublisher:Item.ImageBackgroundColor");
  string image = i.GetString("ItemPublisher:Item.Image");

  string tagclasses = i.GetString("ItemPublisher:Item.Tags.TagName");
  tagclasses = tagclasses.Replace(",", " ");

  string imgwidth = "624";
  string imgheight = "351";

  foreach (var img in (i.GetLoop("ItemPublisher:Item.ProjectImages"))){
    string projectimage = img.GetString("ItemPublisher:Item.ProjectImages.File");
  }

  <figure class="col-3-12 col-small-6-12 no-p no-m hover @tagclasses">
      <a href="@url" class="imglink">
          <img src="/Admin/Public/GetImage.ashx?Image=@image&amp;Width=@imgwidth&amp;Height=@imgheight&amp;Background=@backgroundcolor&amp;Crop=0" alt="@title" class="img-responsive full-width"/>
      </a>
  </figure>

}
tempalte.jpg

Replies

 
Claus Kølbæk
Claus Kølbæk
Reply

Hej Jens

I am not 100% certain I understand your problem but how about something like this:

 

@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>

@foreach (var i in (GetLoop("ItemPublisher:Items.List"))){

  string title = i.GetString("ItemPublisher:Item.Title");
  string description = i.GetString("ItemPublisher:Item.Description");
  string link = i.GetString("ItemPublisher:Item.Link");
  string url = i.GetString("ItemPublisher:Item.Url");
  string backgroundcolor = i.GetString("ItemPublisher:Item.ImageBackgroundColor");
  string image = i.GetString("ItemPublisher:Item.Image");

  string tagclasses = i.GetString("ItemPublisher:Item.Tags.TagName");
  tagclasses = tagclasses.Replace(",", " ");

  string imgwidth = "624";
  string imgheight = "351";
  bool projectHasImages = false;
  foreach (var img in (i.GetLoop("ItemPublisher:Item.ProjectImages"))){
    string projectimage = img.GetString("ItemPublisher:Item.ProjectImages.File");
    if(!string.IsNullOrWhiteSpace(projectimage)){
     projectHasImages = true;
     break;
    }
  }
 string imagesrc = "/Admin/Public/GetImage.ashx?Image="+image+"&amp;Width="+imgwidth+"&amp;Height="+imgheight"+&amp;Background="+backgroundcolor+"&amp;Crop=0";
  <figure class="col-3-12 col-small-6-12 no-p no-m hover @tagclasses">
      @if(projectHasImages){
      <a href="@url" class="imglink">
          <img src="@imagesrc" alt="@title" class="img-responsive full-width"/>
      </a>
     } else {
        <img src="@imagesrc" alt="@title" class="img-responsive full-width"/>
     }
  </figure>

}

 

I could write the answer with abit more eloquent coding aswell, but to keep it easy to understand I think the format is ok.

 
Jens Mouritzen
Jens Mouritzen
Reply

Basicly i just want to check wether this loop is empty or not: 

i.GetLoop("ItemPublisher:Item.ProjectImages")

Then make the if else statement.

Does this make sense?

 
Mario Santos Dynamicweb Employee
Mario Santos
Reply
This post has been marked as an answer

Hi Jens,

If you use i.GetLoop("ItemPublisher:Item.ProjectImages").Any() will check if the loop has records and returns a boolean statement. 

BR, Mario

Votes for this answer: 1
 
Jens Mouritzen
Jens Mouritzen
Reply

That worked! Thanks. It was much simpler than i thought. Here is the code i got now. Thanks Claus and Mario.

 

@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>

@foreach (var i in (GetLoop("ItemPublisher:Items.List"))){

  string title = i.GetString("ItemPublisher:Item.Title");
  string description = i.GetString("ItemPublisher:Item.Description");
  string link = i.GetString("ItemPublisher:Item.Link");
  string url = i.GetString("ItemPublisher:Item.Url");
  string backgroundcolor = i.GetString("ItemPublisher:Item.ImageBackgroundColor");
  string image = i.GetString("ItemPublisher:Item.Image");

  string tagclasses = i.GetString("ItemPublisher:Item.Tags.TagName");
  tagclasses = tagclasses.Replace(",", " ");

  string imgwidth = "624";
  string imgheight = "351";
  
  bool hasimages = i.GetLoop("ItemPublisher:Item.ProjectImages").Any();

  if(hasimages){
  <figure class="col-3-12 col-small-6-12 no-p no-m hover @tagclasses">
    <a href="@url" class="imglink">
        <img src="/Admin/Public/GetImage.ashx?Image=@image&amp;Width=@imgwidth&amp;Height=@imgheight&amp;Background=@backgroundcolor&amp;Crop=0" alt="@title" class="img-responsive full-width"/>
    </a>
  </figure>

  } else {

  <figure class="col-3-12 col-small-6-12 no-p no-m hover @tagclasses">
    <img src="/Admin/Public/GetImage.ashx?Image=@image&amp;Width=@imgwidth&amp;Height=@imgheight&amp;Background=@backgroundcolor&amp;Crop=0" alt="@title" class="img-responsive full-width"/>
  </figure>

  }

}
 
Claus Kølbæk
Claus Kølbæk
Reply

I see - I thought that a projectImage could esist without a value in File due to your first foreach loop :)

As a note I would recommend that you move the if sentence inside the <figure> so you only need that part of the code once - it's simple matter of as little redundency as possible :)

 

You must be logged in to post in the forum