Developer forum

Forum » Templates » Add a loop to a template

Add a loop to a template


Reply

I would like to create a custom loop in my template. What I have done is:

I created product variants in DynamicWeb eCom, for each variant I have selected an image. On my productlist page, I would like to be able to show these images using template tags (loop), so I created a class which inherits from the ProductListExtender. In this class I created the following code:

 

VariantOptionCollection voc;
TagCollection tc;
Loop voLoop;
 

 

 

voc = VariantOption.getVariantOptions("VARGRP1");

voLoop = new Loop();
voLoop.Name = "VariantImages";

foreach (VariantOption vo in voc)
{
    tc = new TagCollection();
    tc.Add(new Tag("VariantID", vo.ID));
    tc.Add(new Tag("VariantImage", vo.ImgSmall));

    voLoop.Tags.Add(tc);
}

Template.Loops.Add(voLoop);

 

base.ExtendTemplate(Template);

 

When I output the available tags (DwTemplateTags) I can see there is a loop available with the name VariantImages.

When I output the available tags within that loop, I don't get any results. When debugging, I can see the loop is filled with tags, but they are not abailable in the template.

 

Has anybody got any idea what I am doing wrong to create a custom loop?

 

Thanks in advance,

Jan Michiel Bruijn

 

 

 


Replies

 
Reply

TemplateV2.Template loop = Template.GetLoop("MyCustomLoop");

 

the loop variable now contains a new TemplateV2.Template object, and when you've filled the tags in each iteration, you run loop.CommitLoop();

 

foreach (Item i in MyItems){

loop.SetTag("Item", i.text);

loop.CommitLoop();

}

 
Reply

Hm... You added something, while I was writing:) so to use your own code:

 

voc = VariantOption.getVariantOptions("VARGRP1");

TemplateV2.Template voLoop = Template.GetLoop("VariantImages")
 

foreach (VariantOption vo in voc)
{
    tc = new TagCollection();
    voLoop.SetTag("VariantID", vo.ID);
   voLoop.SetTag("VariantImage", vo.ImgSmall);

voLoop.CommitLoop()}

 

 
Reply

Thanks! That's it!

Still another question: Is it possible to extend an existing loop with additional data? I would like to add information about a productvariant to the loop 'VariantCombinations'. What I have is:

 

if (Template.ContainsLoop("Products"))
{
    Template tplProducts = Template.GetLoop("Products");

    if (tplProducts.ContainsLoop("VariantCombinations"))
    {
        Template tplVariantCombinations = tplProducts.GetLoop("VariantCombinations");

        tplVariantCombinations.SetTag("TEST", "Jan Michiel");
        tplVariantCombinations.CommitLoop();
    }

    tplProducts.CommitLoop();
}
 

The tag TEST is added to the template tags, but it is added only once. What I want is to add the tag for each VariantCombination that exists in de loop VariantCombinations.

 

Jan Michiel Bruijn
 

 
Reply

I'm not sure, but I think you could add this using a ProductTemplateExtender rather than a ProductList.

 

You must be logged in to post in the forum