zoukankan      html  css  js  c++  java
  • RazorExtensions Templated Razor Delegates

    原文发布时间为:2011-04-27 —— 来源于本人的百度文章 [由搬家工具导入]

    Templated Razor Delegates

    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

  • 相关阅读:
    java+opencv实现图像灰度化
    java实现高斯平滑
    hdu 3415 单调队列
    POJ 3368 Frequent values 线段树区间合并
    UVA 11795 Mega Man's Mission 状态DP
    UVA 11552 Fewest Flops DP
    UVA 10534 Wavio Sequence DP LIS
    UVA 1424 uvalive 4256 Salesmen 简单DP
    UVA 1099 uvalive 4794 Sharing Chocolate 状态DP
    UVA 1169uvalive 3983 Robotruck 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/handboy/p/7163978.html
Copyright © 2011-2022 走看看