need hook between GetTemplate() and instance.Execute()

Feb 18, 2011 at 10:43 PM

I've been writing some code to attempt to enable MVC Razor-style view compilation and execution with RazorEngine but ran into a big snag with the way that TemplateService.Parse() works.  

MVC allows obtaining the instantiated compiled class object prior to calling Execute() to do the rendering so that it can provide some necessary data to the base class of the template (e.g. ViewData).  

The current design of RazorEngine does not provide a way to configure additional information on the object instance before Execute() is called.  Would it be possible to add in hooks prior to Execute() for doing our own thing with the object?  

        public string Parse(string template, string name = null)
            var instance = GetTemplate(template, null, name);

            SetService(instance, this);

            return instance.Result;

I had considered whether it might be possible to hook an IActivator instance into a DI container to use setter injection or something to create the instances with injected dependencies, but might be nice to just have something directly in the design to do this.'

Has anyone else been successful in creating a WebViewPage-like template base class so that you can execute Html helpers, etc. using code compatible with MVC razor views?  That's ultimately what I'd want.

Feb 21, 2011 at 5:46 PM


Sorry for the late reply. While it is entirely possible to use an IActivator with an IoC container, it may not be everything you need. We could look to introduce some eventing support for the TemplateService, this could potentially be used to raise a custom event where you could cast the ITemplate instance back to your known template type (we'd need to consider models potentially), that way you could then assign additional properties on the template?