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

代码
public static class razor
{
public static string parse(string template, string name = null);
public static string parse<t>(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<t>,后者是带model的情形。
还是官方的例子来看看,

{
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<t> : templatebase<t>
public string touppercase(string name)
return name.toupper();

输出结果为:my name in upper case is: matt。
在我们的mycustomtemplatebase<t>抽象类中我们可以像mvc一样定义一些辅助属性和方法,像html、request、response等辅助类等
有事我们需要自定义一些非modle的非static property给template,我的考虑是在templateservice 中的重写parse方法中初始化action:
public string parse<t>(string template, t model, string name = null,action<itemplate<dynamic>> initaction);