Razor Templating Engine

news/2025/3/14 5:37:48/

       最近遇到html模板解析,我完全可以用MS的T4模板或者是StringTemplate等其他的模板来做,但是出于尝试和对Razor语发的感兴趣,便翻了翻Razor模板的资料,其CodePlex主页http://razorengine.codeplex.com/。

      下面讲的都是一些其主页上面的例子,所以请大家别拍砖。别后面想到有些实际意义的例子在写个具体的运用例子吧。

public static class Razor
{
    public static string Parse(string template, string name = null);
    public static string Parse (string template, T model, string name = null);
    public static void SetLanguageProvider(ILanguageProvider provider);
    public static void SetMarkupParser(MarkupParser parser);
    public static void SetTemplateBaseType(Type type);
}

在Razor这个静态类中最重要的方法当然是我们的Parse方法了,其有两个重载,在第二个重载在我们可以传入Template的Model,了解asp.net mvc都会知道这个Model。同时我们可以用SetLanguageProvider方法传入LanguageProvider(C#、VB)等,SetTemplateBaseType传入模板BaseType(可能是我们的自定义类型)。

1:先来个简单的Template:

static void Main(string[] args)
       {
           string template = "Hello @Model.Name! Welcome to Razor!";
           string result = Razor.Parse(template, new { Name = "World" });        
           Console.WriteLine(result);
           Console.Read();
       }

输出结果:

Hello World! Welcome to Razor!

在这里我们传入的是new { Name = "World" }的匿名对象的Model。

2:内部嵌套方法:

string template = @"@helper MyMethod(string name) {

Hello @name

}

@MyMethod(Model.Name)! Welcome to Razor!";

string result = Razor.Parse(template, new { Name = "World" });

输出同样是上边的结果,但是注意这里的与上面不同的是在{}中间的空格等是不会忽略的。我的理解是同样是一个模板的形式吧。

3:传递模板参数:

在传递参数的情况下我们可以采用自定义类,继承至TemplateBase 或者TemplateBase ,后者是带Model的情形。

还是官方的例子来看看,

static void Main(string[] args)
    {
        Razor.SetTemplateBaseType(typeof(MyCustomTemplateBase<>));

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

        Console.WriteLine(result);
        Console.Read();
    }
}
public abstract class MyCustomTemplateBase : TemplateBase
{
    public string ToUpperCase(string name)
    {
        return name.ToUpper();
    }
}

输出结果为:My name in UPPER CASE is: MATT。

在我们的MyCustomTemplateBase 抽象类中我们可以像MVC一样定义一些辅助属性和方法,像html、Request、Response等辅助类等

有事我们需要自定义一些非modle的非static property给Template,我的考虑是在TemplateService 中的重写Parse方法中初始化Action:

public string Parse (string template, T model, string name = null,Action > initAction);

http://www.ppmy.cn/news/627147.html

相关文章

Blade模板引擎

不限制在view中使用PHP原生代码 section 定义一个片段 yield() 占位符 在 *.blade.php 中引用布局文件 extends(layouts) layouts.blade.php 文件中代码 <!DOCTYPE html> <html> <head><title>轻松学会laravel - yield(title)</title> &l…

好玩的横版射击游戏介绍:Broforce武装原型 for mac

为大家推荐一款非常好玩的横版射击游戏&#xff0c;Broforce武装原型 for mac提供了单人模式和合作模式&#xff0c;每一关都有一个恶魔boss&#xff0c;在武装原型破解版中&#xff0c;玩家将操控角色向沿途的敌人发动攻击&#xff0c;也可以使用机关枪消灭敌人&#xff0c;赶…

Alchemy

步骤&#xff1a;1、下载下载之前&#xff0c;说一下这几个软件的关系&#xff0c;便于理解。看图&#xff0c;就是我的理解。Alchemy下载页面&#xff08;点此直接下载文件&#xff09;&#xff1a; http://labs.adobe.com/downloads/alchemy.htmlJava(JRE)下载页面&#xff1…

A. Arena of Greed

链接&#xff1a;https://codeforces.com/problemset/problem/1425/A Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the games goal is to find the greediest of them all, who will then be crowned king of Compfestnesia. The ga…

Archer

CF上的一道题&#xff0c;就是求赢的概率。 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<iomanip> #include<algorithm> #include<cctype> #include<stack> #include<queue> #inc…

blade

Laravel 框架中的 Blade 模板引擎&#xff0c;很好用&#xff0c;但是在官方文档中有关 Blade 的介绍并不详细&#xff0c;有些东西没有写出来&#xff0c;而有些则是没有说清楚。比如&#xff0c;使用中可能会遇到这样的问题&#xff1a; 1.yield 和 section 都可以预定义可替…

Laravel Blade

Laravel默认使用Blade作为模板引擎&#xff0c;Blade中可使用原生PHP代码输出。Blade模板使用.blade.php作为文件扩展名。Blade模板最终都将被“编译”(正则替换)成原生PHP代码并缓存&#xff0c;除非模板文件被修改否则不会重新编译。 模板引擎需要完成最基本三项功能&#xf…

ARCH++

ARCH: Animation-Ready Clothed Human Reconstruction Revisited 可直接用于动画的穿衣服人体重建 我们提出了一种基于图像的三维化身重建方法ARCH&#xff0c;该方法可以重建具有任意服装风格的3D化身。我们重建的化身是动画就绪和高度逼真的&#xff0c;在输入视图的可见区…