Restructured template mechanism and implemented simple template cache

(bug 57051)
Restructured the template mechanism and implemented a simple template cache storing
whether templates have been generated with specific parameter types already circumventing
expensive validation.

Change-Id: I323b918d026646ddfd501d02a8e1391dadd712e9