If I look at the Razor View Engine, then I see a very nice and concise syntax that is not particularly tied to generating html. So I wonder, how easy would it be to use the engine outside asp.net in a "normal" .net environment for example to generate text, code,...
Any pointer, example, comment or explanation is welcome.
Both RazorEngine and RazorTemplates are already mentioned here, but check out RazorMachine. You can simply point your non-MVC app to a ~/Views folder of (another) existing MVC app, execute with sending proper model and get rendered output on 2 lines of code:
Check RazorEngine, it's a little framework built on top of Razor that allows you to do this.
Take a look at RazorTemplates library. It's more lightweight than RazorEngine library, it's thread-safe and has very nice minimal interface.
Compiling and rendering a template is as simple as two lines of code:
There are two issues here:
<tags>
to determine the transition between code and markup. You can probably use it to generate any text but you might run into issues when your output doesn't match Razor's assumptions about what your intentions are.So for example while this is valid Razor code (because of the
<div>
tag):The following snippet is invalid (because the Hello! is still being treated as code):
However there's a special
<text>
tag that can be used to force a transition for multi-line blocks (the<text>
tag will not be rendered):There is also a shorter syntax to force a single line to transition using
@:
:Generate code or text: you mean like T4 Templates: http://msdn.microsoft.com/en-us/library/bb126445.aspx or codesmith tools?