implement template replace and define