Add support for template contents being functions and optimize template rendering a bit