I wanted to do something similar to this when I was writing a wizard-style script to auto-mount new instances of certain packages.
I took a leaf out of the ACS installer script, and ditched ad_return_template.
ad_return_template
ns_write is your friend 😊.
ns_write