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
Now will Create Couple of Interfaces and Classes to Render The Initialization of Template and Initialization of model
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.
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
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
{
/// <summary>
/// Employee template
/// </summary>
/// <returns></returns>
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
you should try out the T4 template for this work , that is much simple than this.
ReplyDeleteParitosh if you see in the last code template
Deletevar 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
Good, You need to take few sessions and train more people with your wits
ReplyDeletegreat
ReplyDeleteGreat Article
ReplyDeleteASP.NET MVC Training | Dot Net Training in Chennai | Dot Net Interview Questions