Razor模板引擎
2015-10-23 12:57
253 查看
说明:基于ashx和html的示例
top.html:
ashx:
1、用法:
1.1、基本用法:方法Parse,对象@Model
string template = "Hello @Model.Name! Welcome to Razor!"; string result = Razor.Parse(template, new { Name = "World" });
1.2、指令Include,方法Compile
string template1 = "Hello"; string template2 = "@Model.Name This is my sample template, @Include(\"Template1\")"; Razor.Compile(template1, "Template1"); string result = Razor.Parse(template2, new { Name = "Wade" });
1.3、常用语法
选择语句:@if{} 循环语句:@for(int i=0;i<@Model.list.Count;i++){} 代码段:@{ var n = 0; }
1.4、输出@:
可以使用@@,也可以使用html的方式@
1.5、注释:
@<h1>1111</h1>@
2、示例:
test.html:<body> @Include("Top") <h1>@Model.TypeTitle</h1> <h2>@Model.TypeId</h2> <hr /> @{ @*<h1>1111</h1>*@ for(int i=0;i<11;i++) { if(i%2==0) { <span>@i</span> } } } </body>
top.html:
<h1>Head</h1>
ashx:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string top = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "RazorTest/top.html"); Razor.Compile(top, "Top");//替换目标字符串,与Include对应 string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "RazorTest/test.html"); html = Razor.Parse(html, new Model.TypeInfo()//替换对象,与Model对应 { TypeTitle = "zhao", TypeId = 1, }); context.Response.Write(html); }
相关文章推荐
- vs2010 调试 改变程序的执行路径 显示下一语句
- xml 5 个预定义的实体引用
- REST API出错响应的设计
- 学Java涉及到要学习的11个知识
- NYOJ 874 签到
- 程序求等价闭包
- 基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作
- python 参议院文本预处理的一维数组的间隔空间
- webview全屏播放html5的视频
- 360资深产品经理:新人如何成功面试产品经理
- 访问控制权限
- 11.11 双十一 前端教你一键领取天猫千张优惠券 (领前先想想有没有钱花这些优惠券)
- 大型网站架构演化发展历程(转)
- H5移动前端完美布局之padding
- nodejs中exports与module.exports的区别
- mac 生成批处理文件
- 一个软件项目开始应该怎么入手分析,搭建
- Codeforces 589J. Cleaner Robot
- Android 运行 C/C++程序 (使用 arm-linux-gcc 交叉编译环境)
- 用 Graphviz+pvtrace 可视化函数调用