Developer forum

Forum » Templates » Product ProductRelatedGroups loop in Razor

Product ProductRelatedGroups loop in Razor

Dmitrij Jazel
Reply

Hello DW guys, 

 

I am trying to use this code in razor in product template:
@{
            foreach (LoopItem i in GetLoop("ProductRelatedGroups"))
            {
                <h4>@i.GetString("RelatedGroup.Name")</h4>
            <ul>
                @foreach (LoopItem i2 in GetLoop("RelatedProducts"))
                {
                    <li>@i2.GetString("RelatedGroup.Name")</li>
                }           
            </ul> 
            } 
        }
It does not seem to work... not sure if I am doing something wrong or what is the case...
On another hand, if I switch to html template
<!--@LoopStart(ProductRelatedGroups)-->
        <h3><!--@Ecom:Product:RelatedGroup.Name--></h3>
        <ul>
        <!--@LoopStart(RelatedProducts)--> 
            <li><!--@Ecom:Product.ID--></li>
        <!--@LoopEnd(RelatedProducts)-->
        </ul> 
    <!--@LoopEnd(ProductRelatedGroups)-->
This works perfectly only in HTML template
But I tried both in Razor, and still no results.
 
Regards,
Dmitrij

Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

You have to get the inner loop from the outer loop variable, e.g. write

@foreach (LoopItem i2 in i.GetLoop("RelatedProducts")) { … }

Currently, the "Convert to Razor" function doesn't handle this very well.

Best regards,
Mikkel

 

 

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hej Mikkel,

hmm. I just tried your example, and it does not parse... Sorry if I am missing something out, but one of the things that puzzles me is <b>i.GetLoop</b> in your code, it used to be just "GetLoop" why you have to surround it with <b> tag?

Another thing is I can see that you have i2  and i variables, I tried to addapt this to my template, but it does not parse.

@foreach (LoopItem i in <b>GetLoop</b>("RelatedProducts")) {
     <span>test</span>
}

I also tried this correct <b>i.GetLoop</b>.

@foreach (LoopItem i in GetLoop("RelatedProducts")) {
     <span>test</span>
 }

It parses, but that is exactly the same code I wrote the first time.

 What am I doing wrong?

 

 
Mikkel Ricky
Reply
This post has been marked as an answer

I was trying to highlight the part you should notice in particular. The b element should not be there.

You have to write 

@foreach (LoopItem i2 in i.GetLoop("RelatedProducts")) { … }

 

 

 

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hello again Mikkel,

Well this is strange but it can't parse your example.

I just tried to use your example, and I got the same parsing error...

This is how I used it:

@foreach (LoopItem i2 in i.GetLoop("RelatedProducts")) { 
    <span>test</span>
}

 

 

 

 

 

 
Mikkel Ricky
Reply

Please show the parsing error you get. 

 
Dmitrij Jazel
Reply

TemplateCompilationException in template (eCom/Product/Product.cshtml): eCom/Product/Product.cshtml

Unable to compile template. Navnet i finnes ikke i gjeldende kontekst

Other compilation errors may have occurred. Check the Errors property for more information.

ved RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context)
ved RazorEngine.Templating.TemplateService.CreateTemplateType(String razorTemplate, Type modelType)
ved RazorEngine.Templating.TemplateService.GetTemplate[T](String razorTemplate, Object model, String cacheName)
ved RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName)
ved RazorEngine.Razor.Parse[T](String razorTemplate, T model, String cacheName)
ved Dynamicweb.Rendering.Template.Output()

Line numbers:
Ln: 1120, cln: 30 - Navnet i finnes ikke i gjeldende kontekst)

 
Mikkel Ricky
Reply

My template is not a complete template, but only shows the change you have to make to your original template to get the inner loop from the outer loop variable.

 
Dmitrij Jazel
Reply

Ahh ok I see, I just missed "i." before get loop in the inner loop. :-)

so now I have this:

@foreach (LoopItem i in GetLoop("ProductRelatedGroups"))
{
  <h4>@i.GetString("RelatedGroup.Name")</h4>
  <ul>
    @foreach (LoopItem i2 in i.GetLoop("RelatedProducts")) { 
      <span>test</span>
    } 
  </ul> 
} 

And I still am not able to see the "ProductRelatedGroups" in Razor, if I switch to HTML template, than it works just fine, but if I am in Razor I am in DwTemplateTags tags I am getting:

27 <!--@LoopStart(ProductRelatedGroups)--> <!--@LoopEnd(ProductRelatedGroups)-->  0

 

 

I remember there was an issue with Razor template loops, that affected related products and product custom fields.

Application version: 8.3.0.6

 

 
Mikkel Ricky
Reply

This template works for me in both 8.3.0.* and 8.3.1.*:

@foreach (var group in GetLoop("ProductRelatedGroups")) {
<h3>@group.GetValue("Ecom:Product:RelatedGroup.Name")</h3>
<ul>
	@foreach (var product in group.GetLoop("RelatedProducts")) {
	<li>@product.GetValue("Ecom:Product.ID")</li>
	}
</ul>
}
 
Martin Skov Nielsen
Reply

Note that you have to remove the <b> tag in order to make it work

 

You must be logged in to post in the forum