toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 259 forks source link

(Documentation request) How does parse from string work? #56

Closed grokky1 closed 7 years ago

grokky1 commented 7 years ago

The IRazorViewEngine has "get" and "find" methods for parsing views from cshtml files.

This library has an incredibly useful "parse from string" function.

I've looked into the code and can't figure out how it does it. I assume that in order to leverage what's in the framework, it writes the string to a temporary file, then loads it as usual. Unless it's a custom engine?

Since this is a specialised feature that is not available in the framework, please consider documenting it so we can make informed decisions about whether to use it, or rely on cshtml files instead?

For example, how is a string converted to a view? Is it a customised engine, or is it written to file first? If so, does it read the file every time? Is it cached, etc.? High level stuff only.

Thanks for this very useful library.


(Doesn't need to be part of the docs, even some quick notes in this issue would help.)

toddams commented 7 years ago

IRazorViewEngine is a part of ASP.NET MVC, not Razor itself, and it is not used in RazorLight at all. Templates are generated by Razor template engine that accepts a single string, so your assumption about temp file is wrong, as it's simply not needed.

When parsing a file - you can identify a file, as it's virtual path is unique, so it can be cached. That what happens when you use "Parse" method. Your templates are only compiled once and then taken from cache.

Strings, however, are not cached which may cause (it already does) low performance. This is a problem of the initial architecture decision, that was made not so well, but it's gonna change at 2.0 ver.

As a temporary workaround I'd suggest to write your templates to files, if performance is critical for you

grokky1 commented 7 years ago

@toddams Thanks for the explanation. String parsing performance is not great, but it's good enough for our needs.

Looking forward to 2.0!