Can I use nested for loops (or similar) to generate scripts?

You cannot write scripts from jinja or yaml. Well can’t is a strong word. Let’s say if you are asking the question, you do not know jinja well enough to pull it off.

Perhaps yaml anchors will help, or custom template macros.
Some ninja jinja stuff for your reading: