These are chat archives for Antaris/RazorEngine

19th
Jun 2015
hoborg91
@hoborg91
Jun 19 2015 09:11
@matthid Thank you for the response. The example is the following.
Given the file (or string) containing @model M <h2>@Model.Name</h2> @foreach(var x in Model.Parts) { <div>@x</div> }. Besides that I need to produce a common HTML (e. g., <h2>A</h2> <div>a</div><div>b</div> for model { Name = "A", Parts = new[] { "a", "b", }, }), I would like also get some template proper for JavaScript compilation. For this example it may be a JavaScript function m(Model) { var result = "<h2>" + Model.Name + "</h2>"; for(var i = 0; i < Model.Parts.length; i++) { result += "<div>" + Model.Parts[i] + "</div>"; } return result; }. Or it may be an HTML filled with tokens of one of the JavaScript templating frameworks: <h2><% Model.Name %></h2> <% for(var i = 0; i < Model.Parts.length; i++) { %> <div> <% Model.Parts[i] %> </div> <% } %>.
In the first case I will supply JSON model to the function and it will produce the same HTML as was produced after Razor template rendering. In the second case I would first compile the given template with appropriate JavaScript framework, obtain the similar JavaScript function and do the same thing.
So in general I would like to convert a template marked with one syntax (Razor) to a template marked with another syntax (e. g., http://underscorejs.org/#template ).
Matthias Dittrich
@matthid
Jun 19 2015 09:18
I don't think this is "doable" in general, you need to consider that you can write any C# code in a Razor template, so you need to be able to convert any C# code to javascript code (or the <% %> thing). You may be able to do it for a specific subset by implementing your own ICompilerService in RazorEngine.
Matthias Dittrich
@matthid
Jun 19 2015 09:24

That said I probably would suggest a webservice instead, keeping the templates on the server (Razor) and calling that service via javascript instead of a client side templating solution.

I have never heard of somebody doing anything similar so you are probably completely on your own (no need to say that it sounds like a really difficult/impossible thing to do)

hoborg91
@hoborg91
Jun 19 2015 09:46

@matthid

any C# code

Since one follows the ViewModel template, usually anything except direct properties access and basic code control constructions (if-else, for, foreach, switch) are not expected. Thus the task is becoming easier.

calling that service via javascript instead of a client side templating solution

You are right, but I would like to minimize the load on the server and transfer some work to the client side. And simultaneously I would like to consider a situation when JS is blocked by the client.

never heard of somebody doing anything similar

I have found the project https://github.com/jchadwick/RazorClientTemplates, but it is abandoned by the main contributor and thus it is obsolete.

So, I intend to inherit from RazorEngine.Compilation.CompilerServiceBase and implement... yet do not know what. Would you be so kind to point me to the reference or say what should I do after implementation? I have expected some parser class where an instance of type ICompilerService must be supplied. Am I correct?

Matthias Dittrich
@matthid
Jun 19 2015 10:18

Ideally you want to intercept somewhere near: https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs#L356

And the cleanest thing to do would be to create your own "RazorCodeLanguage" class, which outputs JS instead of C# (but that's probably more work than to simply walk over the C# tree) and change the property here: https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs#L107.

You should start by compiling from source and marking the methods you want to overwrite as virtual (if needed) and implement what you are trying to achieve. I would certainly accept a pull request later on. You should figure out exactly what you want to do and what you expect after compiling a template. The C# code? the JS Code? both? And how to integrate that with the remaining architecture of RazorEngine (for the start it is probably enough to have a CompilerService that outputs JS and run it standalone whenever you need it)

hoborg91
@hoborg91
Jun 19 2015 10:34
@matthid Thanks for the directions. I shall investigate this approach.
Matthias Dittrich
@matthid
Jun 19 2015 10:40
Hope it helps :)