go-text-template 1.0.7-pr-35

This is a prerelease version of go-text-template.
There is a newer version of this package available.
See the version list below for details.
dotnet add package go-text-template --version 1.0.7-pr-35
                    
NuGet\Install-Package go-text-template -Version 1.0.7-pr-35
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="go-text-template" Version="1.0.7-pr-35" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="go-text-template" Version="1.0.7-pr-35" />
                    
Directory.Packages.props
<PackageReference Include="go-text-template" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add go-text-template --version 1.0.7-pr-35
                    
#r "nuget: go-text-template, 1.0.7-pr-35"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package go-text-template@1.0.7-pr-35
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=go-text-template&version=1.0.7-pr-35&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=go-text-template&version=1.0.7-pr-35&prerelease
                    
Install as a Cake Tool

text/template

NuGet

This project is a C# adaptation of Go's template engine. It began as an experiment to see whether OpenAI Codex could port the Go implementation to .NET. Claude.AI helped with explanations and refinements along the way.

The original Go package can be found here:

Currently this repository demonstrates a relatively small but functional implementation. The TemplateEngine.Process helper reads templates using the ANTLR-generated GoTemplateLexer and GoTemplateParser and performs variable substitution, loops and conditionals.

Features

  • Replace {{ variable }} placeholders with values from dictionaries or model objects.
  • Conditional blocks with if, else if and else clauses.
  • for loops and Go-style range loops over arrays, collections and maps.
  • Built-in functions: eq, ne, numeric comparisons (lt, le, gt, ge), logical operators (and, or, not).
  • Access nested properties, map keys and indexes, including dynamic indexing via variables.
  • Whitespace trimming with {{- and -}} and comment syntax {{/* ... */}}.

Example Scenarios

1. Variable Interpolation

  • Access struct fields: {{ .Field }}
  • Nested field access: {{ .User.Name }}
  • Index arrays or slices: {{ .Items[0] }}
  • Access map entries: {{ .Data.key }}
  • Control whitespace: {{- .Name -}}

2. Conditional Statements

  • Basic if blocks: {{ if condition }}...{{ end }}
  • if/else blocks: {{ if condition }}...{{ else }}...{{ end }}
  • else if chains: {{ if condition }}...{{ else if other }}...{{ end }}
  • Supported conditions include:
    • Boolean fields: {{ if .IsActive }}
    • Comparisons: {{ if eq .Status "active" }}
    • Existence checks: {{ if .User }}

3. Loop Statements

  • Iterate slices or arrays: {{ range .Items }}...{{ end }}
  • Capture index/value: {{ range $i, $v := .Items }}...{{ end }}
  • Iterate maps: {{ range .Map }}...{{ end }}
  • Map key/value variables: {{ range $k, $v := .Map }}...{{ end }}
  • Handle empty collections: {{ range .Items }}...{{ else }}...{{ end }}

4. Built-in Functions

  • Equality: eq, inequality: ne
  • Numeric comparisons: lt, le, gt, ge
  • Logical operators: and, or, not

5. Comments

  • Use {{/* comment */}} to embed comments in templates

Not Implemented Yet

  • Pipelines and chaining of actions.
  • with, define, template and block directives.
  • Custom functions beyond basic comparisons and boolean operators.
  • Custom delimiter support.

Usage

var tmpl = "Hello {{ .Name }}!";
var result = TemplateEngine.Process(tmpl, new { Name = "World" });
Console.WriteLine(result); // Hello World!

Example Template

string letter = @"Dear {{ .Name }}
{{ if .Attended }}
It was a pleasure to see you.
{{ else }}
Sorry you couldn't make it.
{{ end }}
You brought: {{ for item in Items }}{{ item }},{{ end }}
Thank you for the lovely {{ .Gift }}.";

var output = TemplateEngine.Process(letter, new
{
    Name = "Bob",
    Gift = "toaster",
    Attended = false,
    Items = new[] { "book", "pen" }
});
Console.WriteLine(output);

See the unit tests for more examples covering loops, conditionals and range expressions.

Claude's suggestions

https://gist.github.com/yetanotherchris/c80d0fadb5a2ee5b4beb0a4384020dbf.js

License

This project is released under the MIT license. Source code was produced by OpenAI Codex with assistance from Claude.AI. This README was written using OpenAI Codex.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.70 182 6/15/2025
1.0.67 179 6/15/2025
1.0.64 149 6/15/2025
1.0.62 159 6/15/2025
1.0.60 159 6/14/2025
1.0.57 172 6/14/2025
1.0.55 178 6/14/2025
1.0.53 177 6/14/2025
1.0.51 185 6/14/2025
1.0.48 185 6/14/2025
1.0.46 188 6/14/2025
1.0.43 318 6/12/2025
1.0.41 315 6/12/2025
1.0.37 311 6/12/2025
1.0.35 313 6/12/2025
1.0.30 298 6/12/2025
1.0.28 298 6/12/2025
1.0.27-pr-47 306 6/12/2025
1.0.26-pr-47 308 6/12/2025
1.0.25-pr-47 310 6/12/2025
1.0.24-pr-47 307 6/12/2025
1.0.23-pr-46 302 6/12/2025
1.0.22 308 6/12/2025
1.0.21-pr-44 300 6/12/2025
1.0.20 307 6/12/2025
1.0.19-pr-43 304 6/12/2025
1.0.18 307 6/12/2025
1.0.17-pr-42 304 6/12/2025
1.0.16 292 6/12/2025
1.0.15-pr-41 303 6/12/2025
1.0.14 312 6/12/2025
1.0.13-pr-40 304 6/12/2025
1.0.11 314 6/12/2025
1.0.10-pr-38 306 6/12/2025
1.0.9 313 6/12/2025
1.0.8-pr-36 302 6/12/2025
1.0.7-pr-35 313 6/12/2025
1.0.4 296 6/12/2025
1.0.3-pr-34 307 6/12/2025