Rewrite template handling to not use two passes and to be statically compiled.