First steps to allow precompilation to NHaml

Currently i made some tests on my private branch. The problem with this is that there is no way to get the model type and the view – layout combinition before the controller was executed.

So what i did to solve this. Ive added a new markup to NHaml with name MetaMarkupRule. This markup is implemented as property bag and this property’s are passed to the parser at compile time instead of runtime.

The idea is the following: In the view Ive put the a line like this (i currently don’t know if ~ is the best char for this):


Which results in a model property with value System.String which is passed to the Meta Dictionary inside of the TemplateParser.

The next step is to put a generic base type to the TemplateCompiler like this typeof(MyCustomView<>). Then direct after the parse step and right before the compiler melts the source and compiles it, we read the value of the model property if it is available (otherwise the default is System.Object) and create the model type with MakeGenericType of the base type. The result then looks like MyCustomView<System.String> which is this what you currently get if you call return View(“myview”,”mycustommodelstring”).

This could be the way to go for precompilation. After the precompilation return View(“myview”,”mycustommodelstring”) would not longer create the type, instead it searches in the list of the precompiled views and raises an exception if it dose not found the matching view type.

To solve the view-layout combinition problem we simply compile a version auf each view/layout combination that exists or we allow the view to specify also the layout. It is also an idea to allow a view specifiy more then one model. This could work as long as the both model types have the same propertys which are used in the view on it.

2 thoughts on “First steps to allow precompilation to NHaml”

  1. I will do, but later. Currently there is only an application which creates and output the compiled template. Not the compiled class. And the implementation is not good, i only want to see if it will work.

Comments are closed.