Design and implement a template engine – part 1
Recently, I got a task which is typical in many systems: Template with merge fields. The common usage is for email.
Given that an employee is created, the system will notify the manager by email with template:
Dear Manager,
New employee {Name} has been created on {DateTime.Now}.
From the above statement, we have template with 2 merged fields: Employee Name and Created Date. There are some solutions. But I name the old way of doing it here for reference:
Replace merge fields using string.Replace or Regex.
You can imagine it and how to do it. Or you event did it some times.
A better approach: Take advantages of Razor Engine.
The template will be in razor syntax
Dear Manager,
New employee @Model.EmployeeName has been created on @DateTime.Now.ToString(“s”).
In the next post, I will discuss about implementation.