Refactor template engine as library