title: How to Html escape template fields
body: <p>There's a few posts including <a href="https://stackoverflow.com/questions/14331081/what-is-the-best-way-to-ensure-html-entities-are-escaped-in-stringtemplate">this</a> regarding Html escaping but this is not working for me. If I have a simple template such as this:</p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;$field$&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>I need only the field to be escaped, not the whole template. I've created a custom render which uses the System.Web.HttpUtility class to perform the escaping of strings:</p>
  
  <pre><code>class HtmlRenderer : IAttributeRenderer
  {
      public string ToString(object obj, string formatString, System.Globalization.CultureInfo culture)
      {
          return HttpUtility.HtmlEncode(
              new StringRenderer().ToString(obj, formatString, culture));
      }
  }
  </code></pre>
  
  <p>And some sample code to render the template with some data:</p>
  
  <pre><code>public static string Render()
  {
      var group = new TemplateGroup('$', '$');
      group.RegisterRenderer(typeof(string), new HtmlRenderer());
      var template = new Template(group, "&lt;html&gt;&lt;body&gt;$field$&lt;/body&gt;&lt;/html&gt;");
      template.Add("field", "Chalk &amp; Cheese");
      return template.Render();
  }
  </code></pre>
  
  <p>Returns the following:</p>
  
  <pre><code>&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;Chalk &amp;amp; Cheese&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;
  </code></pre>
  
  <p>which escapes everything.</p>
  
  <p>How can I escape only the fields added to the template?</p>