title: HTML is being rendered as literal string using RazorEngine. How can I prevent this?
body: <p>I'm trying to generate a HTML document with RazorEngine (<a href="http://razorengine.codeplex.com/" rel="noreferrer">http://razorengine.codeplex.com/</a>). Everything is <em>mostly</em> working, but the problem I have now is some of the HTML is being rendered correctly, and HTML that I have nested in that is being rendered as literal HTML, so rather than the browser displaying the tables &amp; divs as expected, it's displaying e.g. </p>
  
  <pre><code> "&lt;table&gt;&lt;/table&gt;&lt;div&gt;&lt;/div&gt;"
  </code></pre>
  
  <p>I kick off this process by calling the following:</p>
  
  <pre><code>string completeHTML = RazorEngine.Razor.Parse("InstallationTemplate.cshtml", new { Data = viewModels });
  </code></pre>
  
  <p>The <code>completeHTML</code> is then written to a file.</p>
  
  <p>"InstallationTemplate.cshtml" is defined as:</p>
  
  <pre><code>@{
      var installationReport = new InstallationReport(Model.Data);
  }
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;        
          &lt;div&gt;
              &lt;!-- I would expect this to write the rendered HTML
                   in place of "@installationReport.DigiChannels()" --&gt;
              @installationReport.DigiChannels()    
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Where <code>InstallationReport</code> and <code>DigiChannels</code> are defined as follows:</p>
  
  <pre><code>public static class InstallationReportExtensions
  {
      public static string DigiChannels(this InstallationReport installationReport)
      {
          return installationReport.GetDigiChannelsHtml();
      }
  }
  
  public class InstallationReport
  {
      public string GetDigiChannelsHtml()
      {
          // the following renders the template correctly
          string renderedHtml = RazorReport.GetHtml("DigiChannels.cshtml", GetDigiChannelData());
          return renderedHtml;
      }
  }
  
  public static string GetHtml(string templateName, object data)
  {
      var templateString = GetTemplateString(templateName);
  
      return RazorEngine.Razor.Parse(templateString, data);
  }
  </code></pre>
  
  <p>After <code>GetDigiChannelsHtml()</code> runs and returns <code>renderedHtml</code>, the line of execution returns to <code>TemplateBase.cs</code> into the method <code>ITemplate.Run(ExecuteContext context)</code>, which is defined as:</p>
  
  <pre><code>    string ITemplate.Run(ExecuteContext context)
      {
          _context = context;
  
          var builder = new StringBuilder();
          using (var writer = new StringWriter(builder)) 
          {
              _context.CurrentWriter = writer;
  
              Execute(); // this is where my stuff gets called
  
              _context.CurrentWriter = null;
          }
  
          if (Layout != null)
          {
              // Get the layout template.
              var layout = ResolveLayout(Layout);
  
              // Push the current body instance onto the stack for later execution.
              var body = new TemplateWriter(tw =&gt; tw.Write(builder.ToString()));
              context.PushBody(body);
  
              return layout.Run(context);
          }
  
          return builder.ToString();
      }
  </code></pre>
  
  <p>When I inspect <code>builder.ToString()</code>, I can see it contains proper HTML for the <code>InstallationTemplate.cshtml</code> stuff, and escaped HTML for the <code>DigiChannels.cshtml</code> stuff. For example:</p>
  
  <p><img src="https://i.stack.imgur.com/NCZk6.png" alt="enter image description here"></p>
  
  <p>How can I get <code>@installationReport.DigiChannels()</code> to include the correct HTML instead of the escaped HTML that it's currently doing?</p>