Razor is a templating idea from the ASP.NET team at Microsoft that shipped with their MVC 3 release. Razor differs from other templating languages in that there is no special template syntax to learn, and no keywords. It’s also pretty clean, for a template. Here is an example of a template (C#) that loops through an array:
Vash works on node.js as well as in the browser!
Quick Razor Primer
Razor has a single key character, @. This character has three primary usages:
@( someVar == true ? "yeah <strong>buddy</strong>" : 'nope.' )
In addition, there are two other usages that aren’t very common:
- Denote a “server-side” comment:
@* this text is not included in the compiled template *@
- Escape a literal character in content:
@}would allow the
}to be included literally, and not assumed to be the closing brace of a code block. This is how Vash treats this case, but this may not be compliant with Razor.
Occasionally, Razor might get confused between what’s markup and what’s code. The following example will fail to compile:
Razor has no way of knowing that
I isn’t some JS. So you need to give Razor a hint, using the
This will render as:
I am text! I span many lines!
Occasionally you want to tell Razor that only one line is markup and not code:
Line breaks are of no consequence. To Vash, the following statements are equivalent (and yes, Vash is smart enough to know what’s code and what’s markup!):
Things related to Razor as a View Engine are not implemented. This includes: LayoutPage, RenderSection, RenderBody, @helper, @section, @using, etc.
Inline templates as parameters are not supported. This requires more of a View Engine or framework.
However, because JS is awesome, you can do some interesting things…
Now why would you want to do this? I don’t know, but you can!
Include vash.min.js on your page somewhere. Compile templates:
Return a template string:
For inline script tag usage, I recommend
type="text/vash". For template files on the server or otherwise, I’ve been using a
More information on usage can be found at Vash’s Github repo.
It’s also available using
Let me know what you think! This is definitely my first non-trivial parser experience, and I have a ton to learn.