RazorExtensions Templated Razor Delegates
原文发布时间为:2011-04-27 —— 来源于本人的百度文章 [由搬家工具导入]
David Fowler turned me on to a really cool feature of Razor I hadn’t realized made it into 1.0, Templated Razor Delegates. What’s that? I’ll let the code do the speaking.
@{
Func<dynamic, object> b = @<strong>@item</strong>;
}
<span>This sentence is @b("In Bold").</span>
That could come in handy if you have friends who’ll jump on your case for using the bold tag instead of the strong tag because it’s “not semantic”. Yeah, I’m looking at you Damian
. I mean, don’t both words signify being forceful? I digress.
Note that the delegate that’s generated is a Func<T, HelperResult>. Also, the @item parameter is a special magic parameter. These delegates are only allowed one such parameter, but the template can call into that parameter as many times as it needs.
The example I showed is pretty trivial. I know what you’re thinking. Why not use a helper? Show me an example where this is really useful. Ok, you got it!
Suppose I wrote this really cool HTML helper method for generating any kind of list.
publicstaticclassRazorExtensions {
publicstaticHelperResult List<T>(thisIEnumerable<T> items,
Func<T, HelperResult> template) {
returnnewHelperResult(writer => {
foreach(var iteminitems) {
template(item).WriteTo(writer);
}
});
}
}
This List method accepts a templated Razor delegate, so we can call it like so.
@{
var items = new[] { "one", "two", "three" };
}
<ul>
@items.List(@<li>@item</li>)
</ul>
As I mentioned earlier, notice that the argument to this method, @<li>@item</li> is automatically converted into a Func<dynamic, HelperResult> which is what our method requires.
Now this List method is very reusable. Let’s use it to generate a table of comic books.
@{
var comics = new[] {
new ComicBook {Title = "Groo", Publisher = "Dark Horse Comics"},
new ComicBook {Title = "Spiderman", Publisher = "Marvel"}
};
}
<table>
@comics.List(
@<tr>
<td>@item.Title</td>
<td>@item.Publisher</td>
</tr>)
</table>
This feature was originally implemented to support the WebGrid helper method, but I’m sure you’ll think of other creative ways to take advantage of it.
If you’re interested in how this feature works under the covers, check out this blog post by Andrew Nurse.
http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
RazorExtensions Templated Razor Delegates的更多相关文章
- Razor - 模板引擎 / 代码生成 - RazorEngine
目录 Brief Authors Official Website RazorEngine 的原理 - 官方解释 安装记录 Supported Syntax (默认实现支持的语法) 测试记录 - ca ...
- 警惕!高版本VS发布时预编译导致Mono中Razor找不到视图
早前一段时间,一位朋友在Q群里面找到我,说它按照<Linux.NET学习手记>的操作,把一个ASP.NET MVC 4.0的项目部署到Mono之后出现Razor无法找到视图的现象.当时费了 ...
- ASP.NET MVC 5 Web编程4 -- Razor视图引擎
Razor简介 Razor是ASP.NET新增的一个视图引擎,由微软全球最年轻的副总裁,有着"ASP.NET之父"称呼的Scott Guthrie主导的团队开发. 主导Razor开 ...
- .NET MVC Razor模板预编译(二)
在前面一片文章:<.NET MVC4 Razor视图预编译(一)> 里面我采用的是PrecompiledMvcViewEngineContrib组件进行预编译视图的虚拟地址注册,但是这个组 ...
- .NET MVC4 Razor视图预编译(一)
在平时使用.NET MVC中不乏有类似的需求:某些razor视图,特别是系统后台的视图,不想让用户自行更改,需要通过某种方法把视图模板编译到项目的dll中去. 但是VS并不提供razor的预编译,如果 ...
- MVC5学习系列--Razor视图(一)
前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...
- ASP.NET Core 中文文档 第四章 MVC(3.2)Razor 语法参考
原文:Razor Syntax Reference 作者:Taylor Mullen.Rick Anderson 翻译:刘怡(AlexLEWIS) 校对:何镇汐 什么是 Razor? Razor 是一 ...
- Razor基础语法一
目录: 什么是Razor? 渲染HTML Razor语法 隐式 Razor 表达式 显式 Razor 表达式 什么是Razor? Razor是基于服务端代码转换成网页的标记语法.语法主要包括Razor ...
- VS2015突然报错————Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper
Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with ...
随机推荐
- 【前端_js】解决ajax跨域请求数据
1.ajax发送请求必须遵循同源策略,即请求方和相应方的协议头.域名.端口全部一样.只要三者有一个不一样都视为跨域,浏览器出于安全考虑不允许跨域访问. 解决ajax跨域访问的常用方法: a.使用jso ...
- LAMP 一键部署
LAMP 一键部署 部署http #!/bin/bash ### global variables export lamp_repo=http://192.168.1.5/lamp/ export l ...
- mysql面试题:字段中@之前字符相同且大于等于2条的所有记录
公司发了一张面试题给我,题目如下: 在test数据库中有个flow_user表,找出email字段中@之前字符相同且大于等于2条的所有记录 答案: select substring_index(`em ...
- B1013 数素数(20分)
B1013 数素数(20分) 令 \(P_i\)表示第 i 个素数.现任给两个正整数 \(M≤N≤10^4\),请输出 \(P_M\)到 \(P_N\)的所有素数. 输入格式: 输入在一行中给出 M ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
- [Bzoj2588]Count on a tree(主席树+LCA)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- JVM垃圾回收--年轻代、年老点和持久代(转)
关键字约定 Young generation –>新生代 Tenured / Old Generation –>老年代 Perm Area –>永久代 年轻代: 所有新生成的对象 ...
- Group Policy Object Editor
Group Policy Object Editor The Group Policy Object Editor is a tool that hosts MMC extension snap- ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- leetcode 【 Subsets II 】python 实现
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...