extract templating to function