Is it possible to register templates without specifying the model type?
Currently it works like so (taken from: documentation):
//Code to get template string from file xxx.cshtml into template variable
Razor.Compile(template, typeof(SomeModel), "complex");
However I would like to precompile the templates taking the model type definition from the @model or @inherits directive in the view. Is this possible?
Razor.Compile doesn't require a model type to be specified when compiled.
public class ActualType { public string Firstname {get;set;}}
Razor.Compile("@Model.Firstname", "outputname");
Razor.Run<ActualType>(model, "outputname");
Should run for you just fine.
The @model declaration isn't available yet. Currently there's a patch for it on the list of patches page ( http://razorengine.codeplex.com/SourceControl/list/patches ), however it only adds design time support and not runtime. The current reason for this is because the MVC RazorEngine already does this and we didn't want confusion as to whether this razor engine is the same as the MVC razor implementation (a lot of people think that)
We're looking at implementing it for v2.2 when we both have time to work on it.
As long as the model you pass in matches the required parameters in use by the template you might be good to go.
It actually shouldn't be too difficult to modify the patch to force the template to use the @model declaration as it's template<t>. I could work on it tonight possibly if it's a serious requirement. I'm curious as to why you don't want to include it in the compilation declaration unless you're worried about changing models or you don't know the model beforehand...but in the end you always know the type before you pass it in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With