HtmlTypeProvider 0.9.1

dotnet add package HtmlTypeProvider --version 0.9.1
                    
NuGet\Install-Package HtmlTypeProvider -Version 0.9.1
                    
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="HtmlTypeProvider" Version="0.9.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HtmlTypeProvider" Version="0.9.1" />
                    
Directory.Packages.props
<PackageReference Include="HtmlTypeProvider" />
                    
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 HtmlTypeProvider --version 0.9.1
                    
#r "nuget: HtmlTypeProvider, 0.9.1"
                    
#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 HtmlTypeProvider@0.9.1
                    
#: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=HtmlTypeProvider&version=0.9.1
                    
Install as a Cake Addin
#tool nuget:?package=HtmlTypeProvider&version=0.9.1
                    
Install as a Cake Tool

HtmlTypeProvider

An F# type provider that parses HTML templates with ${HoleName} holes and generates strongly-typed builder APIs that produce HTML strings.

Inspired by Bolero's templating system, but completely independent of Blazor/WebAssembly. Output is plain string via StringBuilder — no runtime framework dependencies.

Installation

dotnet add package HtmlTypeProvider

Quick Start

Inline HTML

type Greeting = HtmlTypeProvider.Template<"<h1>Hello, ${Name}!</h1>">

let html = Greeting().Name("World").Render()
// "<h1>Hello, World!</h1>"

File-based templates


<html>
<head><title>${Title}</title></head>
<body>
  <h1>${Title}</h1>
  <div class="${BodyClass}">${Content}</div>
  <template id="Card">
    <div class="card">
      <h2>${CardTitle}</h2>
      <p>${CardBody}</p>
    </div>
  </template>
</body>
</html>
type Page = HtmlTypeProvider.Template<"templates/page.html">

let html =
    Page()
        .Title("My Site")
        .BodyClass("container")
        .Content(
            Node.Fragment [|
                Page.Card().CardTitle("Card 1").CardBody("Hello").Elt()
                Page.Card().CardTitle("Card 2").CardBody("World").Elt()
            |])
        .Render()

Hole Types

Holes are detected by their position in the template:

Position Example Generated Method Notes
Text content <p>${X}</p> .X(value: string) / .X(value: Node) HTML-encoded string or composable Node
Single attribute value <div class="${X}"> .X(value: string) / .X(value: int) / ... Typed overloads: string, int, float, bool, obj
Mixed attribute <div class="a-${X}"> .X(value: string) String interpolation in attribute
Full attribute <div attr="${X}"> .X(value: Attr) / .X(value: Attr list) Dynamic attribute(s)

The same hole name can appear multiple times — types are merged automatically.

Escaping $ (JavaScript, shell scripts, etc.)

To output a literal ${...} — for example a JavaScript template literal inside a <script> tag — escape the dollar by doubling it: $${ renders as ${ and is not treated as a hole.

<script>
  const greeting = `Hello $${user.name}, you have $${count} messages`;
</script>

renders as:

<script>
  const greeting = `Hello ${user.name}, you have ${count} messages`;
</script>

The rules, applied left to right:

Template Output Notes
${Name} hole value A hole, as usual
$${Name} ${Name} Escaped — literal text, no hole
$$${Name} $ + hole value Literal $ followed by a real hole
$${any text} ${any text} Escape also silences the malformed-hole error
$$ (not before {) $$ Dollars away from { never need escaping

In short: in a run of dollars immediately preceding {, each $$ pair collapses to one literal $; if one $ remains, it starts a hole. Escaping works everywhere holes do: text content, attribute values, rawText templates, and runtime template overrides.

API Reference

Builder Methods

Every template type has:

  • Constructor MyTemplate() — creates a new builder with default (empty) hole values
  • Hole setters .HoleName(value) — fluent methods, return the builder for chaining
  • .Elt() — returns a Node for composition with other templates
  • .Render() — returns the final HTML string

Node Module

Node.Empty()                            // No output
Node.Text "safe text"                   // HTML-encoded text
Node.RawHtml "<b>raw</b>"              // Unencoded passthrough
Node.Elt "div" [| attrs |] [| kids |]  // Element with attrs and children
Node.Fragment [| node1; node2 |]         // Sibling nodes without wrapper
Node.Render node                        // Node -> string

Attr Module

Attr.Make "name" (box "value")   // name="encoded-value"
Attr.Flag "disabled"             // Boolean attribute (no value)
Attr.Attrs [| attr1; attr2 |]   // Group multiple attrs
Attr.Empty()                     // No output

Nested Templates

Use <template id="Name"> inside your HTML to define sub-templates. They become nested types:

type Page = HtmlTypeProvider.Template<"page.html">

// Access nested template as Page.Card
let card = Page.Card().CardTitle("Hi").Elt()

Raw Text Templates

With rawText=true the template is treated as plain text instead of HTML: no HTML parsing, no encoding of hole values, no nested <template> support. This is useful for LLM prompts, emails, config files, or any non-HTML text with ${Hole} placeholders:

templates/greeting.txt:

Hello ${Name}, welcome to ${Place}!
type Greeting = HtmlTypeProvider.Template<"templates/greeting.txt", rawText=true>

let text = Greeting().Name("Alice").Place("Wonderland").Render()
// "Hello Alice, welcome to Wonderland!"

Inline raw text works too — a string that doesn't resolve to an existing file is used as the template itself:

type Prompt = HtmlTypeProvider.Template<"You are a ${Role} at ${Company}.", rawText=true>

Note: raw text hole values are substituted verbatim (not HTML-encoded). .Elt() wraps the result in Node.RawHtml, so only compose it into HTML if the content is trusted.

Runtime Template Overrides

Raw text templates get a second constructor that accepts a replacement template at runtime, while keeping the compile-time typed API. This lets you load edited templates from a database or config without recompiling:

type Greeting = HtmlTypeProvider.Template<"templates/greeting.txt", rawText=true>

// Compile-time template
Greeting().Name("A").Place("B").Render()
// "Hello A, welcome to B!"

// Runtime override — same holes, different text
let fromDb = "Greetings ${Name}! You are in ${Place}."
Greeting(fromDb).Name("A").Place("B").Render()
// "Greetings A! You are in B."

The override is validated on construction: it must contain exactly the same holes as the compile-time template — no missing holes, no extras — otherwise the constructor throws ArgumentException listing the mismatch. Escaped holes ($${...}) in the override render literally and don't count toward validation.

Parameters

type T = HtmlTypeProvider.Template<
    pathOrHtml: string,           // File path or inline template string
    optimizePlainHtml: bool,      // Default: true. Collapse hole-free HTML segments
    rawText: bool                 // Default: false. Treat template as plain text (no HTML parsing/encoding)
>

Troubleshooting

High CPU usage in IDE with .NET 10 / F# 10

If you experience sustained high CPU from fsautocomplete (the F# language server) when using this provider with F# 10, it is caused by an FCS bug in the type subsumption cache where TypeStructure.GetHashCode performs O(n) hashing on every cache lookup.

Workaround: Set the environment variable FSharp_CacheEvictionImmediate=1 before launching your editor. This switches the cache eviction strategy from a background worker (which continuously rehashes) to synchronous inline eviction, eliminating the CPU spike while keeping the cache functional.

On macOS (persists until reboot):

launchctl setenv FSharp_CacheEvictionImmediate 1

On Linux/Windows, set the variable in your shell profile or system environment variables.

After setting it, fully restart your editor (not just reload).

Safety

  • All text content holes are HTML-encoded via System.Net.WebUtility.HtmlEncode
  • All attribute values are HTML-encoded
  • Node.RawHtml and PlainHtml from template source pass through unencoded
  • Void elements (br, img, input, etc.) render as self-closing HTML5

License

Apache 2.0

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on HtmlTypeProvider:

Package Downloads
Lit.Server.Unofficial

Renders Fable.Lit templates to HTML on .NET, with no Fable and no JavaScript runtime. The same `html $"..."` view compiles to lit in the browser and to HTML on the server. Part of the unofficial Fable.Lit fork.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.1 253 8/12/2026
0.9.0 186 7/10/2026
0.8.0 1,629 3/2/2026
0.6.1 188 2/28/2026
0.6.0 121 2/28/2026
0.5.0 124 2/28/2026
0.4.0 136 2/27/2026
0.3.0 131 2/27/2026
0.2.2 140 2/27/2026
0.2.1 141 2/26/2026
0.2.0 136 2/25/2026
0.1.0 133 2/24/2026