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 ...
随机推荐
- Python 正则表达式 匹配任意字符
.(句点)匹配除了换行之外的所有一个字符, .*(点-星)匹配除了换行外的所有字符 >>> >>> r=re.compile(r'.*')>>> ...
- c#:无限极树形结构
最近一直在研究树形结构菜单,无意中让我弄了出来.先上代码: 首先需要这个的一个类 public class Tree { public int id { get; set; } public stri ...
- windows server 2008解决无法PING通问题
今天安装服务器(server 2008),配置完IP地址后,发现局域网其它电脑无法PING通服务器,测线仪测试链路都正常,网线接别的电脑也正常,以为是网卡问题,于是ping了自己的IP,发现能PING ...
- poj2631 Roads in the North(求树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2941 Accepted: 144 ...
- CART树 python小样例
决策树不断将数据切分成小数据集,直到所有目标变量完全相同,或者数据不能再切分为止,决策时是一种贪心算法,它要在给定的时间内做出最佳选择,但并不关心能否达到最优 树回归 优点:可以对复杂和非线性的数据建 ...
- php和js中数组的总结
php中数组的表示方法:array()或者[] js中数组的表示方法:new array()或者[] 一.php中初始化命名数组 在PHP中声明数组的方式主要有两种:一是应用array()函数声明 ...
- Couchbase II( View And Index)
Couchbase II( View And Index) Views view的作用是从没有结构和半结构的数据对象中抽取过滤需要的信息,并生成相关的index信息,通常生成json数据. vie ...
- win7装python3.6提示api-ms-win-runtime-1-1-0.dll丢失
win7为MSDN下的旗舰版,没有servicepack1那个,刚开始安装python3.6提示必须得安装servicepack1,于是乎到微软官网下了个900mb大小的安装包. https://ww ...
- 如何过滤adb logcat输出
简介: 本文介绍如何在 shell 命令行中过滤 adb logcat 输出的几个小技巧. 开发当中经常看到别人的 log 如洪水般瞬间刷满了屏幕,对自己有用的信息都被淹没了,影响心情也影响效率.下面 ...
- python 学习分享-实战篇简单的ftp
import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...