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?
-
I don't think there's an interpreter, but you may be able to use this: stackoverflow.com/questions/3234003/…Matthew– Matthew2012-08-07 14:16:45 +00:00Commented Aug 7, 2012 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– Deathspike2012-08-07 14:18:14 +00:00Commented Aug 7, 2012 at 14:18
-
And how you suppose client will get this cshtml?dantix– dantix2012-08-07 14:21:49 +00:00Commented Aug 7, 2012 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– Deathspike2012-08-07 14:23:22 +00:00Commented Aug 7, 2012 at 14:23
Add a comment
|
2 Answers
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.
1 Comment
Deathspike
Thanks for coming in and letting me know about this.
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.
3 Comments
Deathspike
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.
Nikola Radosavljević
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'.
Deathspike
The 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.