Getting started
First steps
Getting started with DotLiquid is very easy. A DotLiquid template is rendered in two steps: Parse and Render. For an overview of the DotLiquid syntax, please read formatting.
Template template = Template.Parse("hi {{name}}"); // Parses and compiles the template
template.Render(Hash.FromAnonymousObject(new { name = "tobi" })); // Renders the output => "hi tobi"
The Parse
step creates a fully compiled template which can be re-used.
You can store it in memory or in a cache for faster rendering later.
All parameters you want to use in your DotLiquid templates have to be passed as parameters to the Render
method.
DotLiquid does not know about your C# (or VB.NET) local or instance variables.
Rules for template rendering parameters
Objects that you pass to Template.Render
must satisfy one of the following conditions:
- Type is an integral type (int, string, decimal, etc.) or anonymous type
- Class inherits from
Drop
(more info) - Class implements
ILiquidizable
- Class is decorated with
[LiquidType]
attribute. Note that you must specify the allowed member names. For example:[LiquidType("AllowedMember1", "AllowedMember2")]
. - Type is registered with
Template.RegisterSafeType(Type type, string[] allowedMembers)
- Type is registered with
Template.RegisterSafeType(Type type, Func<object, object> func)