Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
seriouz
@seriouz
Easy use of:
string result = Engine.Razor.RunCompile(template, "input", typeof(Input), inputEl);
string result = Engine.Razor.RunCompile(template, "input", typeof(Input), inputEl2);
string result = Engine.Razor.RunCompile(template, "select", typeof(Select), selectEl);
Is the Engine.Razor. threadsafe? In my enviroment it is possible, that *Engine.Razor.RunCompile(template, "input", typeof(Input), inputEl) is called at the same time.
Matthias Dittrich
@matthid
Basically "yes", we always create a new template instance for every "Run"
If you run into any issue open one on github
seriouz
@seriouz
Okay :) I'll test is the next days (Mass compile ;>)
*it
Any idea if its possible to join this chat via IRC or similiar?
such wow... so style ... wow => https://irc.gitter.im/
Matthias Dittrich
@matthid
yeah gitter is nice :D I love all the integrations they provide :)
seriouz
@seriouz
hi from irc
Matthias Dittrich
@matthid
it seems to work! I gotta go now. Report back if your testing works ;)
seriouz
@seriouz
I#LL do! good night!
Vladimir Shmidt
@vladimir-shmidt
Hi all
Do anybody know how to use RazorEngineService with already compiled views?
There was TemplateService in RazorEngine 3
and i just do something like this
http://codepaste.net/wui5r7
but now TemplateService is obsolet
and produce exception on Run method
any help please...
Matthias Dittrich
@matthid

Yes that's by design: There are really two points here: First is template instance creation, we don't allow it as we might need to save some internal state and therefore don't want to provide an API for it (note you still can define your own type if you need to!)

The other thing is re-using ITemplate instances: We don't allow that anymore for multiple reasons. First its not thread safe and second re-using is really error prone.

There is in fact a bug in your code: nested templates (layouts or includes) are not cached properly but recompiled more than actually required...

Therefore I recommend to not use RazorEngine with the legacy API and instead use the new API (sometimes you need to do/think a bit more, but its for the greater good :)

Do you compile the templates and add them as resources? You can still make that work with a custom caching provider...
Vladimir Shmidt
@vladimir-shmidt
yep with new api i found that paradigm is changed
so i have to implement custom CachingProvider and TemplateManager
also ITemplateKey ITemplateSource and ICompiledTemplate
that is so huge overenginiring for reuse ready templates
Matthias Dittrich
@matthid
Yeah that should work
On the other hand nothing changes on the usage side...
Vladimir Shmidt
@vladimir-shmidt
"Do you compile the templates and add them as resources?" exctly
*exactly :)
Matthias Dittrich
@matthid
Sounds really interesting, is that part of your build?
Vladimir Shmidt
@vladimir-shmidt
probably part of application
we generate source via razor temapltes
sorry for misspell
that razor templates already complied with RazorGenerator tool
Matthias Dittrich
@matthid
well yes it looks over engineered, but on the other hand you could provide your implementations and other people can use it without changing their usage code (only the setup/config), which is a good thing considering the complexity here.
Vladimir Shmidt
@vladimir-shmidt
good point
thanks
Matthias Dittrich
@matthid
and again getting it work bug-free without pitfalls by using ITemplate is almost impossible ;)
Vladimir Shmidt
@vladimir-shmidt
than can toy suggest something else?
*you = toy
Matthias Dittrich
@matthid
what you do now (using the interfaces) should work
*new interfaces
If you run into problems feel free to ask here and wait for an answer or open a new issue on github
Vladimir Shmidt
@vladimir-shmidt
ok try to do and maybe try to request changes :)
Matthias Dittrich
@matthid
Sure. Btw generating the templates as part of the build and including them as resources sounds like a really nice idea! If there is a chance to contribute this back I would gladly help with the integration part, I think quite some people would like to have this :) (as part of RazorEngine or a separate NuGet package)
Vincent Baaij
@vnbaaij
I was looking around for the difference between 3.7.0 and 4.2.0. Can't find it. Does anyone have a link to more info. tia
hoborg91
@hoborg91
Hello. I would like to convert my Razor templates (e. g., in ASP.NET MVC) to JavaScript template functions. For that purpose, I suppose that one should analyze ParserResults which is being returned by ParseTemplate method. Here I am confused by the message saying "This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.". This message goes with all methods and classes necessary for the syntax tree analysis. Therefore I have a question: is there a better way to achive the declared target (to make JS of a Razor) or, if not, why does not RazorEngine freely expose techniques for the syntax tree analysis?
Matthias Dittrich
@matthid
@vnbaaij The only difference is that one is build against Razor4 (4.X) and one against Razor3 and Razor2 (3.X).
Matthias Dittrich
@matthid
@hoborg91 Sorry but its not very clear for me what you are trying to achieve. But you may be mixing up (Microsoft.AspNet.)Razor and RazorEngine. Razor handles the Razor-Template to C# code transformation and RazorEngine handles the rest: C# -> Assembly compilation and loading. IIRC ParserResult is a CodeDom generated C# code for Razor2/3 and some Razor specific stuff in Razor4. So one reason we cannot expose it is because there is no unified way to expose it (RazorEngine "sees" only the C# code as a whole). Can you please give an example of what you want to do?
hoborg91
@hoborg91
@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
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

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)