I am using ASP.NET MVC 4 and would like to know if there is a way to compile a Razor view into JavaScript? This means that I want to create the HTML in JavaScript! This would allow the Controller to respond with either HTML or JSON, depending on the request, allowing me to minimize the bandwidth and CPU requirements while serving pages faster to each client. Is there such a compilation option (where I imagine that HTML helpers are actually ran, before compiling into JavaScript), or, is there a JavaScript interpreter for Razor syntax?
Asked
Active
Viewed 1,548 times
0
-
I don't think there's an interpreter, but you may be able to use this: http://stackoverflow.com/questions/3234003/render-view-programmatically-into-a-string – Matthew Aug 07 '12 at 14:16
-
This would defeat the purpose. The intend is to decrease bandwidth and CPU cycles, building the resulting HTML client-side instead of server-side. – Deathspike Aug 07 '12 at 14:18
-
And how you suppose client will get this cshtml? – dantix Aug 07 '12 at 14:21
-
1Hence I want to compile it into JavaScript and serve it as plain JavaScript. Think of Spark compilation into JS functions, or caching Mustache templates and using the Mustache interpreter to render client-side. It's not an issue to cache an entire View in JS if an interpreter is available, but a compiled one is preferred. – Deathspike Aug 07 '12 at 14:23
2 Answers
1
There's currently a project called RazorClientTemplates
that compiles Razor views into JavaScript.
I use it in an MVC 3 project, with Backbone -- and it works, but support for code blocks and HTML helpers are very limited.

MartinHN
- 19,542
- 19
- 89
- 131
0
There is no way to use Razor itself in JavaScript, but there are several JavaScript templating and MVVM engines.
Take a look at:
- Handlebars (templating engine most MVVM engines are based on)
- KnockoutJS
- EmberJS
Unfortunately, this is more complicated than just reusing your Razor templates.

Nikola Radosavljević
- 6,871
- 32
- 44
-
I am not looking for MVVM, but I am aware of Handlebars (which is basically Mustache, as you can also get this for the ASP.NET MVC). Spark is likely better because it's not just an interpreter, but an actual compiler. I'm still looking into using Razor, though. – Deathspike Aug 07 '12 at 14:44
-
Then you should know better than to ask for Razor for Javascript :) It's basically strongly typed language with helpers and that sort of thing. Even if someone tried, you can't do all that on client side. Never heared of Spark, but if it works on client side I don't see how it can possibly be 'actual compiler'. – Nikola Radosavljević Aug 07 '12 at 14:47
-
1The idea would be to take the Razor template, execute the helpers and change the control flow to use the JavaScript-syntax, while building a HTML string to return. This is a View that is compiled into JavaScript, no dependencies and pure-JS to run in-browser. An interpreter, on the other hand, needs the actual template while a compiled View does not. A lot of view engines are capable of compilation, I'm confused as to why Razor has nothing of the sort. – Deathspike Aug 07 '12 at 14:54