Cutout 0.0.0-beta.2
dotnet add package Cutout --version 0.0.0-beta.2
NuGet\Install-Package Cutout -Version 0.0.0-beta.2
<PackageReference Include="Cutout" Version="0.0.0-beta.2" />
<PackageVersion Include="Cutout" Version="0.0.0-beta.2" />
<PackageReference Include="Cutout" />
paket add Cutout --version 0.0.0-beta.2
#r "nuget: Cutout, 0.0.0-beta.2"
#:package Cutout@0.0.0-beta.2
#addin nuget:?package=Cutout&version=0.0.0-beta.2&prerelease
#tool nuget:?package=Cutout&version=0.0.0-beta.2&prerelease
Cutout
When building source generators there is a requirement to generate source code as a string that has indentation managed correctly. The code also needs to be as fast as possible to not impact users in large repositories.
The recommended approach is to use the IndentedTextWriter . This class is simple to use, but is low level.
A standard template engine is a better approach from a code maintenance perspective, but the performance is not as good.
This aims to provide the best of both worlds.
It's a source generator, so can be used in other source generators without incurring a dependency on a template engine, and it lets you use a simplified version of liquid and any type that implements the basic StringBuilder API.
How to use
Create a static method with the [Cutout.Template]
attribute.
The first parameter is the StringBuilder
like type to write to.
Everything else passed can be used in the template.
using Cutout;
public static partial class MyTemplate
{
private const string Template = """
{% if name == "Bob" %}
Hello Bob
{% else %}
Hello {{ name }}
{% end %}
""";
[Cutout.Template(Template)]
public static partial void MyTemplateMethod(StringBuilder sb, string name);
}
Template Language
Everything that is not between {{
or {@
and }}
or %}
is treated as a
string literal.
Any valid C# expression can be used in the template as it is compiled to C# code. So if something is not working, the compiler will tell you.
It supports the whitespace control characters -
the same as liquid.
One deviation from liquid is that there is only one end keyword, {% end %}
.
The following keywords are supported,
If/Else
The if
keyword is used to conditionally render a block of code.
Everything that is a valid C# boolean expression can be used.
For example,
{% if true %}
Some that is true
{% elseif false %}
Some other thing
{% else %}
The default
{% end %}
For/Each/While
The standard looping keywords are supported. They align to the same keywords in C#.
{% for i = 0; i < items.Count; i++ %}
{{ i }}
{% end %}
{% foreach item in items %}
{{ item }}
{% end %}
{% while true %}
{% end %}
var
, continue
, break
and return
are also supported.
Functions
Any other methods using the [Cutout.Template]
attribute can be called.
This allows for building up complex templates from smaller ones.
The syntax is like so,
{% call MyFunction(1, 2, 3) %}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.0.0-beta.2 | 66 | 8/10/2025 |
0.0.0-beta.1 | 76 | 8/3/2025 |