简体   繁体   English

如何在模板化剃刀代理中包含更多逻辑?

[英]How can I include more logic within a templated razor delegate?

Take the following extension method from Mr Haacked . 请采用Haacked先生的以下扩展方法。

public static class Helpers {
  public static HelperResult RenderSection(this WebPageBase webPage, 
      string name, Func<dynamic, HelperResult> defaultContents) {
    if (webPage.IsSectionDefined(name)) {
      return webPage.RenderSection(name);
    }
    return defaultContents(null);
  }
}

The usage is as follows: 用法如下:

<footer>
  @this.RenderSection("Footer", @<span>This is the default!</span>)
</footer>

Quite handy, but what I would like to do for example is include some business logic within the delegate like this: 非常方便,但是例如,我想在委托中包含一些业务逻辑,如下所示:

<footer>
  @this.RenderSection("Footer", @<span>
               This @if (condition) {  
                        <label>is the</label> 
                      } else {  
                        <label> default!</label> 
                      } 
              </span>)
</footer>

But no matter how I have tried to change the extension I get compilation errors when viewing the page. 但是,无论我如何尝试更改扩展名,在查看页面时都会出现编译错误。

How can I achieve this? 我该如何实现?

David Fowler turned me on to a really cool feature of Razor I hadn't realized made it into 1.0, Templated Razor Delegates. David Fowler让我了解了Razor的一个非常酷的功能,但我还没有意识到它已成为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 Smile with tongue out. 是的,我用舌头看着你达米安微笑。 I mean, don't both words signify being forceful? 我的意思是,两个词都不表示有力吗? I digress. 我离题了。

Note that the delegate that's generated is a Func. 请注意,生成的委托是Func。 Also, the @item parameter is a special magic parameter. 另外,@ item参数是一个特殊的魔术参数。 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. 假设我写了这个非常酷的HTML帮助器方法来生成任何类型的列表。

public static class RazorExtensions {
    public static HelperResult List<T>(this IEnumerable<T> items, 
      Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            foreach (var item in items) {
                template(item).WriteTo(writer);
            }
        });
    }
}

This List method accepts a templated Razor delegate, so we can call it like so. 这个List方法接受模板化的Razor委托,因此我们可以这样称呼它。

@{
  var items = new[] { "one", "two", "three" };
}

<ul>
@items.List(@<li>@item)
</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. 如前所述,请注意,此方法的参数@ <li> @item </ li>会自动转换为Func <dynamic,HelperResult>,这是我们方法所需要的。

Now this List method is very reusable. 现在,此List方法非常可重用。 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>@item.Publisher
  </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. 最初实现此功能是为了支持WebGrid助手方法,但是我敢肯定,您会想到其他创造性的方法来利用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM