A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/adoconnection/RazorEngineCore/wiki/Strongly-typed-model below:

Strongly typed model · adoconnection/RazorEngineCore Wiki · GitHub

RazorEngineCode uses slightly different approach on strongly typed model, instead model it forces you to use template base: either existing RazorEngineTemplateBase<T> or your own that inherits RazorEngineTemplateBase.

With this approach you are not bounded by typical MVC keywords and can easily introduce your own.

IRazorEngine razorEngine = new RazorEngine();
string content = "Hello @Model.Name";

// yeah, heavy definition
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(content);

string result = template.Run(instance =>
{
    instance.Model = new TestModel()
    {
        Name = "Hello",
        Items = new[] {3, 1, 2}
    };
});

Console.WriteLine(result);
public class CustomTemplateBase : RazorEngineTemplateBase
{
    public int A { get; set; }
    public string B { get; set; }
    public new MyModel Model { get; set; }

    public string Decorator(object value)
    {
        return "-=" + value + "=-";
    }
}
string content = @"Hello @A, @B, @Decorator(123) @Model.Membership.Level";

IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate<CustomTemplateBase> template = razorEngine.Compile<CustomTemplateBase>(content);

string result = template.Run(instance =>
{
    instance.A = 10;
    instance.B = "Alex";
    instance.Model = model; 
});

Console.WriteLine(result);

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4