If the list of templates doesn't change after startup, then you could run this once and store the results with the subtemplates references.
cheers,
Torben