Using Razor Templates without MVC (RazorLight)
| August 8, 2018 | in
Formatting text is a common part of software development. Many web pages are just that, some sort of template that is converted into HTML.
In the .NET world, we often use ASP.NET MVC for generating those HTML pages. We often also have a view that is rendered with a model to create an HTML page.
This is something we also do for emails: we need some template, combine it with a model, and generate an HTML email.
There are a lot of template engines out there. You don’t have to use one of the existing template engines; you could write your own. You could manually munge your HTML or you could use the same template engine you use when writing HTML pages with ASP.NET MVC: Razor.
Using the Razor template engine requires a little bit of code and some setup, but luckily there is a NuGet package out there that has done all the hard work for you.
Let’s see it in action.
Install RazorLight NuGet package.
Create an instance of the RazorLight Builder.
Next, we should see if the template has already been compiled. If so we should use the already compiled template.
If the template hasn’t been compiled, we will need to compile it. This will also generate the same HTML as the RenderTemplate call.