Refactor template engine as library

This pull request fixes Trac ticket #17093.
Good idea, I'll use a `getattr`.