Click here to Skip to main content
Click here to Skip to main content

Razor template and Razor Engine

By , 20 Nov 2011
 

Introduction 

We can use Razor template outside MVC project as a content template and generate the real content using RazorEngine  

RazorEngine

A templating engine built upon Microsoft's Razor parsing technology. The RazorEngine allows you to use Razor syntax to build robust templates. Download RazorEngine here( http://razorengine.codeplex.com/

Define Razor Template

TestTemplate.cshtml
<h1>@Model.DisplayName</h1>

Message

@Model.Message

Generate the Real content using RazorEngine 

using RazorEngine;
using System.Dynamic;

namespace HelloConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // read the template
            var template = File.ReadAllText(@"TestTemplate.cshtml");

            // create template data
            dynamic myObject = new { DisplayName = "Mahesh", 
			          Message="Sample Message" };            

            // generate the content using razor engine
            string result = Razor.Parse(template,myObject);

            // display the result
            Console.WriteLine(result);

            Console.ReadLine();
        }
    }
} 

 

Conclusion

I hopes you got some idea about how to use razor template outside mvc project using RazorEngine. This is the same as my original post Razor template outside MVC project.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

A Mahesh
Web Developer
India India
Symphony Services, Banglore, 560 087, India
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1 [modified] PinmvpSacha Barber20-Nov-11 20:01 
GeneralMy vote of 1 PinmemberMohammad_Denmark18-Nov-11 3:52 
GeneralMy vote of 1 PinmemberThomas Freudenberg16-Aug-11 0:15 
GeneralMy vote of 1 PinmemberSteven Berkovitz15-Aug-11 10:22 
GeneralRe: My vote of 1 PinmvpSacha Barber20-Nov-11 20:01 
SuggestionBetter suited as a Tip/Trick PinmentorDaveAuld13-Aug-11 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 21 Nov 2011
Article Copyright 2011 by A Mahesh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid