Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 578 forks source link

Add alternative TemplateSource implementations with streaming support. #265

Open djechelon opened 9 years ago

djechelon commented 9 years ago

Hi, I can see no straightforward way to load a template from a file. You can supply a string as a template payload, but if that template (most common case) is in a file within the application or on a separate disk folder, then you have to first read the file in-memory into a string.

I won't bother you with my specific usage case, but consider reporting or mailing services based either on application resources or files on disk.

It could be a great idea to extend the API to load a template from a path or a stream

Thanks

matthid commented 9 years ago

Actually we support that (even throughout the API), its just a bit hidden:

We have the ITemplateSource interface which you can implement. And anywhere where you currently specify the in-memory string template you can use your own implementation. Basically we only access the GetTemplateReader method and you can decide if you want to buffer the string in the Template property, which is accessed for debugging purposes. The TemplateFile is also only accessed for debugging purposes, so if you put a meaningful name there you will simplify your life. And one more thing: It is possible that RazorEngine fetches the template multiple times (if you compile with different model-types), therefore you probably want to cache if you use a NetworkStream or something like that.

Note that if you use an ITemplateManager the Resolve method returns ITemplateSource as well.

I was just a bit lazy providing a default implementation, so I would be happy to accept a contribution here :) If you run into trouble, let me know.

djechelon commented 9 years ago

I don't think a FileSystemTemplateSource and AssemblyTemplateSource are big tasks, so let me say "maybe". I don't have much time for my free-time projects anyway :-)

One more interesting thing could be an implementation for Spring.net, in order to create a template repository from Context.xml. But that is another point.