Console application-Using Razor Engine to fill template

Now a days we all are hearing about Razor Engine in MVC ,People are using the Razor syntax to build there Web Application.

We are going to use a template engine built upon Microsoft’s Razor parsing technology.you can download it form  http://razorengine.codeplex.com/#

Here we will see how we can fill the predefined template values,using the model .

One scenario where it is useful is say you have a multiple template that needs to be filled on the basis of the model supplied and then we need to send a email.

Lets see how to use the Razor Engine to fill the given template that will be displayed on the Window.Here i am using the console application.

Create the following model

 namespace RazorEngine_Implementation  
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public int PinCode { get; set; }
public int Mobile { get; set; }
}
}

Now will Create Couple of Interfaces and Classes to Render The Initialization of Template and Initialization of model

 namespace RazorEngine_Implementation  
{
public interface ITemplate
{
string GetTemplate(TemplateType template);
}
}

 namespace RazorEngine_Implementation  
{
internal interface IRendorTemplate
{
///
/// Employee template
///

///
string CreateTemplate(TemplateType type);
}
}

 namespace RazorEngine_Implementation  
{
public class Template : ITemplate
{
public string GetTemplate(TemplateType template)
{
switch (template)
{
case TemplateType.Employee:
{
return "Employee Details Id=@Model.Id,Name=@Model.Name,Address1=@Model.Address1," +
"Address2=@Model.Address2,Pin=@Model.PinCode,Mobile=@Model.Mobile";
}
}
return string.Empty;
}
}
}

 namespace RazorEngine_Implementation  
{
public enum TemplateType
{
Employee
}
}

 namespace RazorEngine_Implementation  
{
public class RendorEmployeeTemplate : IRendorTemplate
{
public string Template { get; set; }
public Employee Employee { get; set; }
public RendorEmployeeTemplate()
{
Template = CreateTemplate(TemplateType.Employee);
}
public string CreateTemplate(TemplateType type)
{
Employee= InitializeEmployee();
var template = new Template();
return template.GetTemplate(type);
}
public Employee InitializeEmployee()
{
return new Employee()
{
Id = 1,
Name = "David",
Address1 = "North Hill",
Address2 = "DC",
Mobile = 7651132,
PinCode = 87658
};
}
}
}

Next we will Create a Console application and in the Main() will will put the following code and we will add the Reference of the RazorEngine and if you have created the Separate DLL for the above code add its reference.

  var template = new RendorEmployeeTemplate();  
Console.WriteLine(Razor.Parse(template.Template, template.Employee));
Console.ReadLine();

In the above code we you can see Razor.Parse(Template,Model). this is the function that has been exposed by razor DLL.

Happy Coding

Published by Nirbhay

Technology Geek,Blogger,Reviewer

3 thoughts on “Console application-Using Razor Engine to fill template

  1. Paritosh if you see in the last code template
    var template = new RendorEmployeeTemplate(); Console.WriteLine(Razor.Parse(template.Template, template.Employee));
    we just need to do this and pass the template and model.

    In the above example i have shown using interface and templates so you might feel its is complicated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: