Using files as templates

May 17, 2011 at 1:59 PM

I've been looking around the forum and the documentation but can't work out a simple way to use Razor templates by loading the templates from a file.

I have a Web Forms app and want to store my Razor templates in files (.cshtml) and then call them from a code behind page to send an email.

Could anyone point me in the right direction?

Coordinator
May 17, 2011 at 3:15 PM

The simplest way is just to load the file as a string and then use the razor engine to parse that.

 

string template = System.IO.File.ReadAllText(filename);

string output = Razor.Parse(template);

 

From there it's just a matter of passing in your model and setting your filename properly.