A T4 based View rendering engine for ASP.NET MVC - Shows how to create a View Engine for ASP.NET MVC, leveraging the Text Template (T4) infrastructure already out there for rendering the view during runtime.
Clarification: Here, I’m not using T4 for design time code generation. We are using T4 toolkit to render the views during runtime
Read more details:
- Creating a custom View Engine for ASP.NET MVC leveraging Text Template (T4) engine for rendering the view
This project contains.
- A ViewEngine implementation
- A view engine by implementing the IViewEngine interface. There is an abstract class VirtualPathProviderViewEngine that already implements IViewEngine interface and provides some extra functionality for path detection of view files. VirtualPathProviderViewEngine
has two methods we are concerned about – CreateView and CreatePartialView from where we should return a custom View, that has the path information to the template file (*.tt) to render.
- A View
- A view, by implementing the IView interface. IView has a Render method that we are interested about. We’ll read and render the tt file, leveraging the TT engine
- A T4 Host
- A custom T4 host, by implementing the ITextTemplatingEngineHost interface, for self hosting the template transformation proces
Normally, as you are aware, Text Templates (T4) are used with in Visual Studio for activities like Code Generation. But T4 engine is a string processor, and you can use it for doing things beyond that, like creating this simple ViewEngine for ASP.NET MVC to
render tt files as views.
How ever, we can’t use the Microsoft.VisualStudio.TextTemplating libraries directly - because I don't believe that T4 can be legally redistributed without Visual Studio. So, I’ve decided to relay on the Mono equivalent T4 implementation, Mono.TextTemplating
(included in the download)