Skip to content

Liquid.NET for Developers

Mike Bridge edited this page Oct 2, 2015 · 37 revisions

Although the Liquid.NET templating language is very close to Shopify Liquid, the Liquid.NET C# code is very different from the Shopify Ruby implementation.

The main things you will need to do in C# are to parse a template, as well as how to write a tag, block or filter.

Getting Started

String myTemplate = "Hello {{ greeting }}";

// Create a context
ITemplateContext ctx = new TemplateContext()
              .DefineLocalVariable("greeting", "World")
              .WithAllFilters();

// Parse the string into a LiquidTemplate
LiquidTemplate ast = LiquidTemplate.Create(templateString);

// Merge the context into the template
String result = template.Render(ctx);

// Show the result
Console.WriteLine(result);

==> 
   "Hello World"

Types

Types in liquid are numeric, boolean, string, collection, hash, date, and the special type range. Any of these can take the value "nil". These are represented with C# classes that implement IExpressionConstant.

liquid C#
boolean BooleanValue
numeric NumericValue
date DateValue
string StringValue
collection ArrayValue
hash DictionaryValue
range* GeneratorValue

Ranges are currently only available in a few places---most of the rest of this discussion doesn't apply to them. More on this later.

Arrays and Dictionaries are heterogeneous, so an array can hold a mix of types, and although the keys of a hash are always strings, a value in a dictionary can be of any type.

A numeric has an underlying type, which is int, long, decimal or BigInteger. You can create a NumericValue with the static constructor NumericValue.Create(val). This means that if you create a numeric value with the decimal "3.00", it will render as "3.00". If you do math with two numeric values, it will attempt to use the precision of the most precise value, so an "int" NumericValue times a "decimal" NumericValue will yield a decimal NumericValue.

Filters

See How to Write a Filter for more details.

Tags

See How to Write a Tag or Block for more details.

Clone this wiki locally