Razor引擎下载地址:

http://github.com/Antaris/RazorEngine

 

解析Model:

  string template = "Hello @Model.Name! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });

 

使用Helper:

string template = 
  @"@helper MyMethod(string name) {
      Hello @name
  }
  @MyMethod(Model.Name)! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });

 

预编译:

string template = "Some really complex template that will take time to parse";
    
    Razor.Compile(template, "complex");
    Razor.Run("complex");
Razor.Compile(template, typeof(SomeModel), "complex");
Razor.CompileWithAnonymous(template, "complex");

 

使用自定义模版:

public abstract class MyCustomTemplateBase<T> : TemplateBase<T>
{
  public string ToUpperCase(string name)
  {
    return name.ToUpperCase();
  }
}

Razor.SetTemplateBase(typeof(MyCustomTemplateBase<>));

string template = "My name in UPPER CASE is: @ToUpperCase(Model.Name)";
string result = Razor.Parse(template, new { Name = "Matt" });

 

相关文章:

  • 2022-03-02
  • 2022-12-23
  • 2022-02-01
  • 2021-06-20
  • 2022-12-23
  • 2022-02-11
  • 2021-09-02
猜你喜欢
  • 2021-11-14
  • 2021-04-13
  • 2022-02-07
  • 2022-01-17
  • 2021-09-05
  • 2021-07-22
相关资源
相似解决方案