C# Razor Templating Engine
Templating engine built upon Microsoft's Razor parsing technology
http://razorengine.codeplex.com/
Add new text file to your project called ‘RazorTemplate.txt’ that looks like that
Hello @Model.Name! Welcome to Razor!
This is a sample program to run the template
class Program
{private static void Main(){var template = File.ReadAllText("RazorTemplate.txt");
var result = Razor.Parse(template, new {Name = "John"});Console.WriteLine(result);}}
and output
Hello John! Welcome to Razor!
another example with custom model
//template
@inherits RazorEngine.Templating.TemplateBase<RazorSample.RazorModel>Hello @Model.Name! Welcome to Razor!//Test
namespace RazorSample
{[TestFixture]class Tests
{[Test]public void Test(){var template = File.ReadAllText("RazorTemplate.txt");
var result = Razor.Parse(template, new RazorModel(){Name="John"});Console.WriteLine(result);}}public class RazorModel{public string Name { get; set; }}}
Example of Loop (not related to the code above)
public class RazorModel{public List<string> Charts { get; set; }public RazorModel()
{Charts = new List<string>();}}<table>@foreach (var chart in @Model.Charts) {<tr><td><img alt="" src="images/@(chart).png" /></td>
</tr>}</table>
Comments
Post a Comment