If you need to iterate or apply conditional logic to generate a config, then you need to use CLI Configlets for that. CLI Configlets are powerful than device templates, but they have a bit of a learning curve.
I recommend starting with the online docs and the example configlets that shipped in your Junos Space.
-- "There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST