Developer forum

Forum » Templates » Getting loop iterations outside loop

Getting loop iterations outside loop

Jens Mouritzen
Jens Mouritzen
Reply

Hi guys
I need some help with this, probably easy to do, but i'm stuck.

I need to get a variable from a loop from outside the loop (razor-template.png). I can only get it to render the first value of the selections from the loop (checkbox list). See rendered-html.png

I need it to render the putputs into some div classes.

What am i missing here? frown

razor-template.png rendered-html.png

Replies

 
Steffen Kruse Hansen Dynamicweb Employee
Steffen Kruse Hansen
Reply

Hi Jens,

I'm not sure I fully understand, what you are trying to do, but the way I understand it, you are trying to write all SolutionType.Option.Labels to the 'label' variable in this format "{Label1} {Label2} {Label3}", right?

If the above it correct, I think your problem is that you put 'i.GetString("ItemPublisher:Item.SolutionType.Option.Label") into the 'label' variable, because this will replace everything from the previous loop-iteration.

So I believe that the code should look like the one in the attachment.

I hope this solves your problem

Best regards,

Steffen

 

 

Code.png
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Jens,

 

I guess what Steffen meant was 

label = optionLabel + " ";

 

Otherwise you only get empty spaces :)

 

Nuno

 
Jens Mouritzen
Jens Mouritzen
Reply

Hi Nuno, thanks. It still only renders the last checkbox in the loop. See attachments. I need to retrieve the variable from the loop outside of the loop.

razor-template.png rendered-html.png
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Jens,

 

I'd say that now the issue is because you are wrapping that around an if statement checking if an option is selected. I would assume you only have 1 selected right now. If you remove the if statement temporarily, you might get all of the expected labels.

 

Best Regards,

Nuno

 
Jens Mouritzen
Jens Mouritzen
Reply

Removed the if statement, now it renders the last option on all the iterations of the items.

rendered-html.png
 
Steffen Kruse Hansen Dynamicweb Employee
Steffen Kruse Hansen
Reply

Hi Jens,

Thanks to Nuno for correcting my error :)

You need to change 'label = optionLabel + " "' to 'label += optionLabel + " "', then it should work as expected.

See full example in attachment

Code_2.png
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Right Steffen,

I missed that one too :)

 
Jens Mouritzen
Jens Mouritzen
Reply

Yes! Thanks guys, i love this forum :)
I added in the if statement again so only the selected options is rendered, now it works and the markup looks like this (attached image)

rendered-html.png

 

You must be logged in to post in the forum