Trim Down Your Hairy HTML Templating Problems with Stubble.NET

In a recent software project, my team came across the need for an HTML template value substitution tool. That set me off on the search for the right tool for the job. Initially, I thought we could get away with using some sort of regex replacement. But, we wanted it to be as efficient as possible, and regex doesn’t really meet that need. That’s when I found the mustache syntax for HTML templates and ultimately settled on Stubble.Net.

What is Stubble.NET?

Stubble.NET is a powerful templating engine that allows you to mix HTML with placeholders for data. You can dynamically populate these placeholders with data from your application. These placeholders are known as “mustaches” due to their curly brace syntax. You can read more about the mustache syntax here.

With Stubble.NET, you can create dynamic HTML content without the need for cumbersome string concatenation or complex formatting. Stubble.NET isn’t the only mustache template library for .NET, though. There’s also Nustache! If you’re not using .NET but still like the sound of the mustache HTML templating, you can find a list of supported languages and their corresponding libraries here.

Getting Started

Getting started with Stubble.NET is pretty straightforward. Begin by adding the Stubble.Core NuGet package to your project. Once installed, you can use it to compile and render templates with your data. The library supports a variety of data formats, including JSON, objects, and dictionaries, making it flexible and versatile. I added the package to my project like so:

<PackageReference Include=”Stubble.Core” Version =”1.10.8”/>

Creating HTML Templates

HTML templates are at the core of what Stubble.NET does. You can create templates as plain HTML files with placeholders for dynamic data using the double curly braces, such as {{ variableName }}. These placeholders act as markers that Stubble.NET will fill in with actual data when rendering the template. In my use case, I passed in the HTML as a string and had the stubble renderer return the populated template as a string. This is how I did it with a dictionary of values to populate the template:

StubbleVisitorRenderer stubble = new StubbleBuilder().Build();
string populatedHtml = stubble.Render(templateHtml, populationValues);

Benefits of Using Stubble.NET

Clean and Readable Templates: Stubble.NET promotes clean and readable HTML templates by separating static content from dynamic data, making your templates easy to maintain. It’s easy to identify the template values by the double curly braces.

Flexibility: The library is highly adaptable and can work with a variety of data sources and formats, making it suitable for a wide range of applications. It also allows you to specify how to handle values that aren’t specified in the population values. By default if a template value isn’t in your population values it replaces that value with an empty string. I was able to alter this using the following solution:

stubble = new StubbleBuilder().Configure(settings => {
settings.AddValueGetter(typeof(object), (key, value, ignoreCase) => “your substitute value here”);

Community and Support: Stubble.NET benefits from an active community and regular updates, ensuring it remains a reliable tool for your projects.

Overall, Stubble.NET is a valuable addition to your .NET toolkit if you need to generate HTML templates with dynamic data. It’s simple, efficient, and compatible with various data formats. That makes it a compelling choice for developers looking to enhance their HTML templating capabilities. Give it a try in your next project, and you’ll quickly see how it simplifies the process of creating dynamic HTML content. Happy templating!


Join the conversation

Your email address will not be published. Required fields are marked *