Scriban is a fast, powerful, safe and lightweight text templating language and engine for .NET, with a compatibility mode for parsing liquid
templates.
// Parse a scriban template
var template = Template.Parse("Hello {{name}}!");
var result = template.Render(new { Name = "World" }); // => "Hello World!"
Parse a Liquid template using the Liquid language:
// Parse a liquid template
var template = Template.ParseLiquid("Hello {{name}}!");
var result = template.Render(new { Name = "World" }); // => "Hello World!"
The language is very versatile, easy to read and use, similar to liquid templates:
var template = Template.Parse(@"
<ul id='products'>
{{ for product in products }}
<li>
<h2>{{ product.name }}</h2>
Price: {{ product.price }}
{{ product.description | string.truncate 15 }}
</li>
{{ end }}
</ul>
");
var result = template.Render(new { Products = this.ProductList });
NOTICE
By default, Properties and methods of .NET objects are automatically exposed with lowercase and
_
names. It means that a property likeMyMethodIsNice
will be exposed asmy_method_is_nice
. This is the default convention, originally to match the behavior of liquid templates. If you want to change this behavior, you need to use aMemberRenamer
delegate
- Very efficient, fast parser and a lightweight runtime. CPU and Garbage Collector friendly. Check the benchmarks for more details.
- Powered by a Lexer/Parser providing a full Abstract Syntax Tree, fast, versatile and robust, more efficient than regex based parsers.
- Precise source code location (path, column and line) for error reporting
- Write an AST to a script textual representation, with
Template.ToText
, allowing to manipulate scripts in memory and re-save them to the disk, useful for roundtrip script update scenarios
- Compatible with
liquid
by using theTemplate.ParseLiquid
method- While the
liquid
language is less powerful than scriban, this mode allows to migrate fromliquid
toscriban
language easily - With the AST to text mode, you can convert a
liquid
script to a scriban script usingTemplate.ToText
on a template parsed withTemplate.ParseLiquid
- As the liquid language is not strictly defined and there are in fact various versions of liquid syntax, there are restrictions while using liquid templates with scriban, see the document liquid support in scriban for more details.
- While the
- Extensible runtime providing many extensibility points
- Support for
async
/await
evaluation of scripts (e.gTemplate.RenderAsync
) - Precise control of whitespace text output
- Full featured language including
if
/else
/for
/while
, expressions (x = 1 + 2
), conditions... etc. - Function calls and pipes (
myvar | string.capitalize
)- Custom functions directly into the language via
func
statement and allow function pointers/delegates via thealias @ directive
- Bind .NET custom functions from the runtime API with many options for interfacing with .NET objects.
- Custom functions directly into the language via
- Complex objects (javascript/json like objects
x = {mymember: 1}
) and arrays (e.gx = [1,2,3,4]
) - Allow to pass a block of statements to a function, typically used by the
wrap
statement - Several built-in functions:
- Multi-line statements without having to embrace each line by
{{...}}
- Safe parser and safe runtime, allowing you to control what objects and functions are exposed
You can install the Scriban Extension for Visual Studio Code to get syntax coloring for scriban scripts (without HTML) and scriban html files.
- See the Language document for a description of the language syntax.
- See the Built-in functions document for the list of the built-in functions.
- See the Runtime document for a description of the .NET runtime API to compile and run templates.
- See the Liquid support document for more details about the support of liquid templates.
- See my blog post "Implementing a Text Templating Engine for .NET" for some behind the scene details.
Scriban is available as a NuGet package:
Compatible with the following .NET framework profiles:
.NET3.5
.NET4.0+
- .NET PCL profile
portable40-net40+sl5+win8+wp8+wpa81
UAP10.0+
NetStandard1.1+
andNetStandard1.3+
running onCoreCLR
Also Scriban.Signed NuGet package provides signed assemblies.
Scriban is blazing fast! For more details, you can check the benchmarks document.
This software is released under the BSD-Clause 2 license.
- dotliquid: .NET port of the liquid templating engine
- Fluid .NET liquid templating engine
- Nustache: Logic-less templates for .NET
- Handlebars.Net: .NET port of handlebars.js
Adapted logo Puzzle
by Andrew Doane from the Noun Project
Alexandre Mutel aka xoofx.