This project attempts to explore and compare the code generation options available in .NET Core, specifically those that are template based and generate code from a given model.
The templates that are included so far are
- Razor Templates (reference docs)
- T4 Templates (reference docs)
- XSL Templates (reference docs)
- Scriban or Liquid Templates (reference docs)
- Mustache Templates (reference docs)
Razor Templates have clean syntax and a pleasure to use, but strangely there is no Razor Engine in .NET 5.0 so one has to rely on a project on a GitHub project https://github.com/adoconnection/RazorEngineCore
- Clean minimal razor syntax
- Supports embedded C# methods
- Most IDEs have great intellisense ,despite being a standalone template
- Not part of the official .NET Core offering
- Much slower than other generators
<h3>FullName: @Model.FullName</h3>
<h3>Skills:</h3>
<ul>
@foreach (var skill in Model.Skills)
{
<li>@skill</li>
}
</ul>
T4 templates have been around a long time, but similar to the Razor Templates there is no offical support in NET 5.0 so one has to rely on a GitHub project https://github.com/mono/t4
- Can embed C# helper methods to aid in code generation
- Faster because the template has a pre-compiled code-behind generated by the IDE
- T4 Syntax Feels outdated syntax compared to razor
- Relies on IDE to generate a precompiled C# code behind
- Not part of the official .NET Core offering
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<h3>FullName: <#= Model.FullName #></h3>
<h3>Skills:</h3>
<ul>
<# foreach (var skill in Model.Skills) { #>
<li><#=skill#></li>
<# } #>
</ul>
Xsl templates have been around for decades and a W3C standard and has been supported since .NET 1.0
- Is a an official W3 standard.
- Supported in from .NET 1.0 to latest .NET and .NET Core
- Most IDEs have great intellisense ,despite being a standalone template
- Geared more for xml-type (includingHTML) output
- Syntax not as simple as Razor
- Have to deal with xml namespaces and verbose syntax
- Cannot embed C# helper methods to aid in code generation
- .NET Core still only suports XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/PersonModel">
<h3>FirstName: <xsl:value-of select="FullName"/></h3>
<h3>Skills:</h3>
<ul>
<xsl:for-each select="Skills/string">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Liquid Templates, like Razor, have clean and minimal syntax. Liquid is an open-source template language created by Shopify and written in Ruby. Scriban project (which supports Liquid templates as well) can be found here: https://github.com/scriban/scriban
- Very fast
- Syntax might get confusing if you're used to Razor syntax
- Can't embed C# helper methods
<h3>FullName: {{ full_name }}</h3>
<h3>Skills:</h3>
<ul>
{% for skill in skills %}
<li> {{ skill }} </li>
{% endfor %}
</ul>
Mustache Templates, are very widely used in html but also for general code generation. They are logic-less templates in that the contain no for-loops or logic A fairly active GitHub repo (at the time of this commit) thta supports Mustache Templates is https://github.com/stubbleorg/stubble
Package | Version | Downloads | LastUpdated |
---|---|---|---|
Stubble | 1.9.3 | 445,197 | 23/05/2020 |
Mustachio | 2.1.0 | 49,492 | 24/06/2019 |
Nustash | 1.16.0.10 | 238,28 | 21/04/2019 |
Mustache-Sharp | 1.0.0 | 194,509 | 15/07/2018 |
*Stats taken from nuget.org on 13 June 2021 |
- Very fast
- No logic can be embedded in the templates
<h3>FullName: {{FullName}}</h3>
<h3>Skills:</h3>
<ul>
{{#Skills}}
<li> {{.}} </li>
{{/Skills}}
</ul>
Engine | Time (ms) |
---|---|
T4 | 3 |
Mustache | 91 |
Liquid | 127 |
Xsl | 303 |
Razor | 2392 |
Package | Repo Stars | Most downloads for a version in the last year |
---|---|---|
Mono.TextTemplating (for T4) | 215 | 6,788 |
Stubble.Core (for Mustache) | 258 | 427,328 |
Scriban (for Liquid) | 1,500 | 86,091 |
Xsl | n/a | n/a (part of .NET Core) |
RazorEngineCore | 205 | 28,379 |