This project is read-only.


Feb 8, 2011 at 8:41 PM

I am new to Razor, just doing some research, and was wondering, why is this project needed (no offense). Trying to figure out where it fits, what Razor lacks, a what is it that this project solves.  Actually, I think this would be a good intro on the home page anyway.

Feb 8, 2011 at 9:04 PM

Lol, well my initial response was going to be "why not?".

RazorEngine is a wrapper around the parser and code generation features of Razor. Razor itself its not the MVC Razor ViewEngine, Razor is a pretty wicked code/markup parser. MVC and Web Pages used Razor a specific way, to generate compilable code from quite a robust template language. There are a number of examples of how to use this parser outside of MVC, and Ben (@BuildStarted) used some example code provided by Andew Nurse (@anurse) to allow you to use the Razor language as a more generalised templating language (think NVelocity, String Template, etc). Since his initial articles, we've expanded on this as a full wrapper around Razor providing templating services. This can go on be used for things like email generation, reporting, etc.  There are some novel ways this is being used, for instance the guys over at Umbraco are using it to provide an alternative macro language provider.

It is by no means a replacement to the MVC ViewEngine (although, with a little work you could do so).

Feb 8, 2011 at 9:10 PM

Great, thanks for a quick answer, I am looking into using razor syntax to generate c# code at the runtime, which would than have to be than compiled (medium trust).  It looks like your project could help me do this get there faster. Do you see any issues I could run into?

Feb 8, 2011 at 9:21 PM

What sort of code are you trying to compile. RazorEngine wraps up string templates into instances of ITemplate, which are dynamically compiled, and executed. From that we get a string result which is merged with a model instance (if provided). It does work in ASP.NET Medium Trust, there is a little configuration to do (

Feb 8, 2011 at 9:32 PM

Pure C# code (meta-data programming). Templates would create strings that represent the class code and than they would be compiled into in memory assemblies. I'll probably use some of the code you have referenced above for that step too (no reason to re-invent the wheel). So first step is to use razor to give me the class code back as a string.

Feb 9, 2011 at 9:52 AM

That's possible, you would need to ensure that your C# class files can be parsed without Razor getting confused as to what is code and what is content.

The real obstacle you will face is compilation of code in a medium trust environment. You can't use the CodeDomProvider (or derivations thereof, e.g. CSharpCodeProvider) directly because that requires full trust. We tackled a similar situation with RazorEngine as Razor uses CodeDomProvider instances internally to generate code, so we had to take advantage of ASP.NET's BuildProvider model to defer compilation to the build system. There is a little work to get that done, but it does work.

Feb 11, 2011 at 6:33 PM

Great, I did not know that in 4.0 VirtualPathProvider works in medium trust, in 3.5 it worked only in full trust :)

Feb 21, 2011 at 8:59 PM

I also hoped to use Razor for generating C# code files (right now I'm using NVelocity). Have you guys had any luck with it? Does the parser get confused? Seems like HTML would be much easier for it to deal with, since the two langs differ so much...

Let me know how far you've gotten.

Feb 21, 2011 at 9:05 PM

Well, we decided to use T4 syntax for templates. We created our own parser and compiler for T4, so that we can grab .tt files and compile them on the fly, but also we can test them in VS. I did not like that I would have to do <text> tags everywhere.

Feb 21, 2011 at 9:12 PM

Thanks for your quick answer. You've pretty much confirmed by suspicions - it's safer to use T4.
Btw, I just learned that Mono has an implementation - Mono.TextTemplating