Reputation: 35
I am trying to use Razor Engine as a standalone template engine. This is working great except that I don't know which are the properties that will be passed to Razor therefore I can't declare an object listing my vars. I currently do this using a dictionary, allowing me to write things like that :
@Model.Vars["MyProperty"]
But I would like to be able to simply write :
@MyProperty
Is there some way to do this ?
Thanks in advance for your answers.
Upvotes: 2
Views: 227
Reputation: 41256
You can use some razor syntax to provide intellisense for your view:
@inherits MyNamespace.MoreNamespace.MyCustomRazorModel<MyModelType>
As long as your MyCustomRazorModel
implements the required Write
, WriteLiteral
etc methods, then everything should work correctly, and you could use the following in your views:
@MyProperty
Assuming you had a definition like so:
public class MyCustomRazorModel<T>
{
public string MyProperty { get; set; }
}
I mention that your backing Razor implementation should probably be Generic simply so that you can re-use it with whatever model you want for templating. That is also how the Razor engine works in ASP.NET, by providing a Generic model that exposes the model in the @Model
property.
EDIT
For example, take a look at the following class to see how the framework guys did it:
http://msdn.microsoft.com/en-us/library/gg402107(v=vs.108).aspx
This is the backing view for the RazorEngine in ASP.NET. For standalone implementations, you would make a backing type for the view to inherit like this.
Upvotes: 1