TemplateParser: deduplicated some code. No functional change intended