Skip to content
Craig edited this page Nov 2, 2018 · 12 revisions

Alt text

Nettle is a .NET Templating Language Engine inspired by Handlebars, designed as a lightweight solution to solving various content rendering problems faced in .NET applications. Typical problems Nettle can be used to solve include:

  • Generating web pages for content management systems
  • Generating emails or notifications
  • Generating administrable XML or CSV exports
  • Generating administrable reports or documents

The main difference between Nettle and Handlebars is that Nettle makes use of functions and variables to enable data to be dynamically loaded (and manipulated) into a template. This could be useful in situations where the model passed to the template is very basic, but a requirement means that some additional, related data needs to be displayed. Instead of having to refactor, build and deploy the C# code (which could also create a code smell), only the template needs to be edited.

Documentation

Quick Start Guide

The following guide covers the basics of Nettle.

Usage

Install the NuGet package:

Install-Package Nettle

The following code will create a Nettle compiler and compile a simple template:

var source = @"Welcome {{Name}}";

var model = new
{
    Name = "John Smith"
};

var compiler = NettleEngine.GetCompiler();
var template = compiler.Compile(source);
var output = template(model);

/* Result:
Welcome John Smith
*/

Nettle Language

The Nettle templating language is simple, there are just six core concepts to learn:

Model Bindings

Model bindings provide a way of inserting the value of a model property or variable into the content. A string representation of the matched property or variable value will replace the binding code block. The syntax for using a model binding is:

{{Name}}

Nested model binding paths are also supported:

{{User.Identity.Name}}

For more information, see the model bindings documentation.

Functions

Functions can take zero or more parameters; a string literal, number, boolean, property or variable. The syntax for using a function is {{@FunctionName(Param1, Param2, ...)}}.

For example:

{{@Truncate("Hello World!", 5)}}

Which would generate:

Hello

For more information, see the functions documentation.

Variables

The value of a property or the result of a function can be assigned to a variable. This gets added to the model and can then be used further down the template. The syntax for creating a variable declaration and assignment is:

{{var message = "Hello World"}}

{{message}}

Which would generate:

Hello World

For more information, see the variables documentation.

Iterators

For each loops are supported with any property or variable that is of type IEnumerable. The syntax for using a for each loop is:

{{each $.RoleAssignments}}
	Role: {{$.RoleName}}
{{/each}}

Which would generate something like:

Role: Admin
Role: Customer

For more information, see the iterators documentation.

Conditions

If statements are supported with any property or variable that is either of type bool or can be resolved as true or false. The syntax for using an if statement is:

{{if $.Active}}
	Currently Active
{{/if}}

For more information, see the conditions documentation.

Partials

Nettle supports partial rendering with registered templates. The syntax for rendering a partial is:

{{> PartialName Model}}

The model is optional and if left empty, the current templates model is used instead. The model can be any value assignable to a variable, including a variable reference.

For more information, see the partials documentation.