0

I know that

the template action allows you to pass in a second argument that will be assigned to the dot (.) argument inside of the template.

-- https://www.calhoun.io/intro-to-templates-p2-actions/

However, I need to loop over my nested template, and I need to pass the current loop number to the nested template, apart from the rest of the existing arguments.

Let me explain with my example -- https://play.golang.org/p/Y7OIxM9t-XU

  • In line 62, you can see that I need a bunch of arguments, e.g. {{.a}}{{.b}}, in my nested template.
  • In line 64, I tried to pass the current loop number $val as the extra to the existing arguments passing to my nested template. That's where it fails -- If I remove line 64, it runs fine.

So, how to pass the current loop number together with the existing bunch of arguments all to the nested template?

My desired output would be:

Ape1 ate Apple1 and SH
Ape2 ate Apple2 and SH
. . .
xpt
  • 20,363
  • 37
  • 127
  • 216
  • 1
    Does https://stackoverflow.com/a/43428608/5728991 answer your question? – Charlie Tumahai Sep 25 '21 at 05:03
  • Ah, yes @CeriseLimón, and yours' better than the accepted one. https://play.golang.org/p/q2_eN-BSA3B... Hold on, I need to pass `.`, to the nested template, and your solution doesn't support that, right sir? – xpt Sep 25 '21 at 13:56
  • Any value can be passed including dot. See example in answer where dot is passed as the “row” argument. – Charlie Tumahai Sep 25 '21 at 14:02
  • Ah, yes, you're right sir. I was only looking at the playground code, whereas your example had it. thx. Problem solved @CeriseLimón. https://play.golang.org/p/3Sy1n-Cj9r3 – xpt Sep 25 '21 at 14:10

0 Answers0