title: How to pass HTML fragment (as a delegate?) to a declarative Razor Helper?
body: <p>I've been writing some declarative <a href="http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx" rel="nofollow">Razor Helpers</a> (using the @helper syntax) for use with the <a href="http://umbraco.codeplex.com/releases/view/62573" rel="nofollow">Umbraco 4.7</a> which now supports the Razor view engine (though I would imagine this applies equally to WebMatrix or ASP.NET MVC). They all work fine. However, I would like to make them a bit more flexible so that I can pass into them an HTML fragment that can be 'wrapped' around the output (but only when there is output). For instance, I have a helper (much simplified here) that can generate an HTML link from some parameters:</p>
  
  <pre><code>@helper HtmlLink(string url, string text = null, string title = null, 
  string cssClass = null, bool newWindow = false)
  {
      if (!String.IsNullOrEmpty(url))
      {
          System.Web.Mvc.TagBuilder linkTag = new System.Web.Mvc.TagBuilder("a");
  
          linkTag.Attributes.Add("href", url);
          linkTag.SetInnerText(text ?? url);
  
          if (!String.IsNullOrEmpty(title))
          {
              linkTag.Attributes.Add("title", title);
          }
  
          if (!String.IsNullOrEmpty(cssClass))
          {
              linkTag.Attributes.Add("class", cssClass);
          }
  
          if (newWindow)
          {
              linkTag.Attributes.Add("rel", "external");
          }
  
          @Html.Raw(linkTag.ToString())
      }
  }
  </code></pre>
  
  <p>Calling <code>@LinkHelper.HtmlLink("http://www.google.com/", "Google")</code> would generate the HTML output <code>&lt;a href="http://www.google.com/"&gt;Google&lt;/a&gt;</code>.</p>
  
  <p>What would be nice, though, is if I could optionally pass in an HTML fragment that would be wrapped around the generated hyperlink HTML so long as the URL has a value. I'd basically like to be able to do something like this:</p>
  
  <pre><code>@LinkHelper.HtmlLink("http://www.google.com/", "Google", @&lt;li&gt;@link&lt;/li&gt;)
  </code></pre>
  
  <p>and get output</p>
  
  <pre><code>&lt;li&gt;&lt;a href="http://www.google.com/"&gt;Google&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>or <code>@LinkHelper.HtmlLink("", "", @&lt;li&gt;@link&lt;/li&gt;)</code></p>
  
  <p>and get no output at all.</p>
  
  <p>I read in Phil Haacked's blog about <a href="http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx" rel="nofollow">Templated Razor Delegates</a> but cannot get the hang of how they can be used in this context - if, indeed, it is possible. I get the feeling I'm missing something or barking up the wrong tree.</p>