Product List:
RazorTemplateEngine
Outline:
Help to parse template based on Razor and Html, ASP.NET MVC is not necessary
Quick Start:
A Complex type Student
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
Inherited Controller
public class DerivedController : TemplateController
{
public ActionResult Action()
{
TemplateData["Title"] = "Hello";
TemplateData["Students"] = new List<Student> {
new Student{ID = 0 ,Name = "Parker Zhou"},
new Student{ID = 1 ,Name = "Sue Kuang"}
};
return Template(@"Template\Action1.cshtml");
}
}
Create 3 cshtml under Template folder
|
Action1.cshtml
@{
string str = "Hello world!";
Layout = @"Template\_Layout2.cshtml";
}
<html>
<head>@TemplateData["Title"]</head>
<body>
<h1>@str</h1>
<table>
@foreach (var s in TemplateData["Students"] as IEnumerable<Student>)
{
<tr><td>@s.ID</td><td>@s.Name</td></tr>
}
</table>
<h2>iiii@@mail.com</h2>
</body>
</html>
|
_Layout2.cshtml
@{
Layout = @"Template\_Layout.cshtml";
}
<h1>I'm Layout2 Title</h1>
@RenderBody()
<h1>I'm Layout2 bottom</h1>
|
_Layout.cshtml
<h1>I'm Layout Title</h1>
<h1>@TemplateData["Title"]</h1>
@RenderBody()
<h1>I'm Layout bottom</h1>
|
Drive the result:
string result = TemplateDriver.Drive(new DerivedController().Action()).ToString();
Console.WriteLine(result);
Result!
<h1>I'm Layout Title</h1>
<h1>Hello</h1>
<h1>I'm Layout2 Title</h1>
<html>
<head>Hello</head>
<body>
<h1>Hello world!</h1>
<table>
<tr><td>0</td><td>Parker Zhou</td></tr>
<tr><td>1</td><td>Sue Kuang</td></tr>
</table>
<h2>iiii@mail.com</h2>
</body>
</html>
<h1>I'm Layout2 bottom</h1>
<h1>I'm Layout bottom</h1>
Configurate additional import namespace if necessary
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="RazorTemplateEngineImportNamespace"
type="RazorTemplateEngine.ImportNamespaceResolver,RazorTemplateEngine"/>
</configSections>
<RazorTemplateEngineImportNamespace>
<add namespace="ModelTest"/>
<add namespace="System.Collections.Generic"/>
</RazorTemplateEngineImportNamespace>
</configuration>
How to customize base page in order to extend Razor function:
public class HtmlExtension
{
public string Raw(string content)
{
return content;
}
}
//always inherit from __TemplatePage
public class CustomeizePage : __TemplatePage
{
private HtmlExtension _html = new HtmlExtension();
public HtmlExtension Html
{
get { return _html; }
set { _html = value; }
}
}
[TestClass]
public class RazorTemplateEngine_CustomizeBasePage
{
[TestMethod]
public void TestMethod1()
{
TestController tc = new TestController();
//Set DefaultTemplateBaseType
TemplateDriver.DefaultTemplateBaseType = typeof(CustomeizePage);
Debug.WriteLine(TemplateDriver.Drive(tc.Action2()));
}
}
then you can use "Html" Object in cshtml file like :
@Html.Raw(string.Format("insert into {0} values ({1},'{2}')","table1",1,"Parker"))
Feature:
- 支持Razor和Html语法(基本的@语法)的模板文件解析
- 支持Layout /@ Renderbody()
- 支持类似动态编译机制,在程序运行期间,如果模板文件变了,可立刻生效
- 支持名字空间引用配置
- 支持复杂的程序集引用关系
- 支持Layout相对路径或绝对路径引用
- 支持自定义扩展页面对象功能
Contact:
parkerzhou2010@gmail.com
Lincense:
Microsoft Public License (Ms-PL)