Added support for recursive templates.

Sorry, it’s a bit messy.