Using Liquid for text-based templates with .NET

The Liquid language

Liquid templates have (usually) the extension `.liquid` and the language is a combination of objects, tags and filters. This is a simple example of text combined with a piece of Liquid. The double braces indicate the Liquid code.

This is sample output of just a string {{ "Hello Liquid!" }}
This is sample output of just a string Hello Liquid!
This is the first table name: {{ model.Tables[0].Name }}
This is the first table name: Claims
{%- comment -%}for loop{%- endcomment -%}
Tables in the model:
{% for table in model.Tables -%}
{{ table.Name }}
{% endfor -%}
{%- comment -%}Split{%- endcomment %}
{% assign teamWindmill = "Laurent, Jan, Carlos, Ibrahim, Bart, Isabel, Martin" | split: ", " %}
Team Windmill has {{ teamWindmill | size }} members:
{% for member in teamWindmill -%}
{{ member }}
{% endfor -%}
{% assign list = model.Patients.Records | where: "gender", "F" -%}
Female Patients:
{% for entry in list -%}
{{ entry.Id }} = {{ entry.Name }} ({{ entry.gender }})
{% endfor -%}

Combining a Liquid template with data

To combine Liquid templates with data, you’ll need to write some code. That’s what we did for our solution. We used .NET 6 in combination with the NuGet package Fluid. The code for the package is open source and can be found on GitHub.

  1. Create a FluidParser
  2. Pre-parse the template into a IFLuidTemplate
  3. Create a TemplateContext where you can add data
  4. Call the Render method on the IFluidTemplate object which outputs a string.
string templateContent = "Some liquid {{ model.Tables[0].Name }}";
...
var parser = new FluidParser();if (parser.TryParse(templateContent,
out IFluidTemplate template,
out string error))
{
TemplateOptions options = new TemplateOptions();
options.MemberAccessStrategy = new UnsafeMemberAccessStrategy();
var ctx = new TemplateContext(
new { model = data }, options, true);
try
{
string output = template.Render(ctx);
}
catch (Exception ex)
{
// handling of parser error
}
}
else
{
// handling of the template parsing error
}

Developing Liquid Templates

Once you have this in place, you need the templates of course. And they can be very big or complex. There is a nice way to use sub components, which is actually a partial template stored in separate files. But still, the work on templates can be hard to test — as you need to parse it through your software solution to see if it works.

Conclusion

Liquid is a very powerful template language that is really useful if you need some kind of mail-merge or quick-find-and-replace processing based on text. As you can store Liquid templates in files, but also in databases, it’s pretty easy to include this in a project.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Martin Tirion

Martin Tirion

15 Followers

Senior Software Engineer at Microsoft working on Azure Services and Spatial Computing for enterprise customers around the world.