Karl Cassar Karl Cassar - 3 months ago 21
C# Question

know if a particular template in RazorEngine is already compiled or not

Is it possible to know if a particular template is already compiled using the RazorEngine? Basically, if you call:

Razor.Parse("Hello there @Model.Name", model, "hello-world");


This would compile the template using the key 'hello-world'. This might take a couple of milleseconds for the first time, but almost instant for the second time round due to caching. Is it possible to know if a template is already compiled? Something like:

var isCompiled = Razor.IsCompiled("Hello there @Model.Name", "hello-world");

Answer

v3.2.0 of RazorEngine includes a ITemplateService.HasTemplate method used to check the cache, but this method doesn't exist on the Razor static type, so to use it you would need to manually instantiate and maintain a TemplateService instance.

Do you actually need to know if they are already cached? I ask because we consider the cache before we start parsing the template, whenever you call ITemplateService.Parse (Razor.Parse).

Comments