Developer forum

Forum » Development » template tag loop inside another loop

template tag loop inside another loop


Reply

I want to create a custom module with a template tag loop inside another template tag loop.

 

The firts loop contains a datarow and then for every datarow there is another loop with datarows

 

How do I create the second loop inside the first one?


Replies

 
Nicolai Høeg Pedersen
Reply
paul wrote:

I want to create a custom module with a template tag loop inside another template tag loop.

 

The firts loop contains a datarow and then for every datarow there is another loop with datarows

 

How do I create the second loop inside the first one?


 

You can have loops in loops like this in code:

 

Dim t As New Templatev2.Template("MyModule/Template.html")
   t.SetTag("HelloText", "Hello there...")
   Dim FirstLoopTemplate As Templatev2.Template = t.GetLoop("FirstLoop")
   For i As Integer = 0 To 10
    FirstLoopTemplate.SetTag("FirstLoopCounter", i)
    Dim SecondLoopTemplate As Templatev2.Template = FirstLoopTemplate.GetLoop("SecondLoop")
    For x As Integer = 0 To 5
     SecondLoopTemplate.SetTag("SecondLoopCounter", x)
     SecondLoopTemplate.CommitLoop()
    Next
    FirstLoopTemplate.CommitLoop()
   Next

   Dim output As String = t.Output

 

 

With a template like this: The template says: <!--@HelloText--><br>
<ul>
<!--LoopStart(FirstLoop)-->
 <li><!--@FirstLoopCounter--></li>
  <ul>
  <!--LoopStart(SecondLoop)-->
   <li><!--SecondLoopCounter--></li>
  <!--LoopEnd(SecondLoop)-->
  </ul>
<!--LoopEnd(FirstLoop)-->
</ul>

 

 
Reply

Hi Nicolai

 

Tryed your example but the second loop is not placed inside the first.

 

Could you tested it?

I converted it to c# but I don't think thats the problem.

 

Don't you have to somehow add the second loop to the first?

 

 
Nicolai Høeg Pedersen
Reply
paul wrote:

Hi Nicolai

 

Tryed your example but the second loop is not placed inside the first.

 

Could you tested it?

I converted it to c# but I don't think thats the problem.

 

Don't you have to somehow add the second loop to the first?

 


 

It seems like its the template that is wrong - missing all the @... The code works. The second loop is added to the first in this line:

 

SecondLoopTemplate As Templatev2.Template = FirstLoopTemplate.GetLoop("SecondLoop")

 

 <!--@HelloText-->
<ul>
 <!--@LoopStart(FirstLoop)-->
 <li>
  <!--@FirstLoopCounter-->
 </li>
 <ul>
  <!--@LoopStart(SecondLoop)-->
  <li>
   <!--@SecondLoopCounter-->
  </li>
  <!--@LoopEnd(SecondLoop)-->
 </ul>
 <!--@LoopEnd(FirstLoop)-->
</ul>

 

 

Dim

 
Nicolai Høeg Pedersen
Reply

And the code in C# (Not tested)

 

Templatev2.Template t = new Templatev2.Template("MyModule/myTemplate.html");
    t.SetTag("HelloText", "Hello there...");
    Templatev2.Template FirstLoopTemplate = t.GetLoop("FirstLoop");
    for (int i = 0; i <= 10; i++) {
        FirstLoopTemplate.SetTag("FirstLoopCounter", i);
        Templatev2.Template SecondLoopTemplate = FirstLoopTemplate.GetLoop("SecondLoop");
        for (int x = 0; x <= 5; x++) {
            SecondLoopTemplate.SetTag("SecondLoopCounter", x);
            SecondLoopTemplate.CommitLoop();
        }
        FirstLoopTemplate.CommitLoop();
    }

 

 

You must be logged in to post in the forum