Skip to content

Liquid.NET for Developers

Mike Bridge edited this page Nov 6, 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.

Getting Started

Parsing a template requires a minimum of three steps:

  1. generate a LiquidTemplate object from a string of raw liquid code
  2. create an ITemplateContext (which maintains the state of the parsing, like variables)
  3. merge the two together using LiquidTemplate.Render(ctx:ITemplateContext)
// Parse the liquid string into a LiquidTemplate
LiquidTemplate ast = LiquidTemplate.Create("Hello {{ greeting }}");

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

// 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.

‼️ Note: the C# classes changed in the 0.9.2 beta release.

liquid C#
boolean LiquidBoolean
numeric LiquidNumeric
date LiquidDate
string LiquidString
collection LiquidCollection
hash LiquidHash
range* LiquidRange

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.

LiquidHashes and LiquidCollections are heterogeneous, meaning that a collection can hold a mix of types, and although the keys of a hash are always strings, a value in a hash can also be of any type.

A numeric has an underlying C# type, which is int, long, decimal or BigInteger. You can create a LiquidNumeric with the static constructor LiquidNumeric.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" LiquidNumeric times a "decimal" LiquidNumeric will yield a decimal LiquidNumeric.

TODO: Describe the Option types.

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