zoukankan      html  css  js  c++  java
  • 在非MVC环境下使用 Razor引擎

    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" });
  • 相关阅读:
    对于作用域和闭包的理解
    响应式开发学习(3)——图片优化
    响应式开发(2)
    响应式开发(1)
    数据结构
    进阶题目
    集合
    数组
    内存相关
    线程
  • 原文地址:https://www.cnblogs.com/ccsharp/p/3180835.html
Copyright © 2011-2022 走看看