Scriptic is a fast, powerful, safe and lightweight text templating language and engine for .NET, with a compatibility mode for parsing liquid
templates.
Scriptic is a direct fork of, and extension to, Scriban and thus inherits all of its awesome features, while adding support for layouts and sections (similar to .NET Razor's) (see below).
The following applies equally to Scriptic and Scriban:
// Parse a scriban template
var template = Template.Parse("Hello {{name}}!");
var result = template.Render(new { Name = "World" }); // => "Hello World!"
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
- 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. Note that the extension does not recognize Scriptic features on top of Scriban.
Scriptic adds a few additional keywords to the scripting language:
layout
- for example, the directive{{layout "_main_layout.htmls"}}
indicates the layout file for the current page. Layouts can be nested, i.e. a layout file can itself have alayout
directive. There can only be onelayout
directive in a given file, though. The whole layout concept is modeled after .NET Razor views.body
- a placeholder inside a layout file that indicates where the page content is going to be rendered.section
- for example{{section "main-menu}"}}
in a page is a named, reusable snippet of HTML and Scriptic. The content of a section is rendered wherever a correspondingrender
directive is placed.render
- for example{{render "main-menu"}}
indicates where a given named section is to be rendered within a layout file. Sections can also be defined and rendered within the same page. The same section can be rendered multiple times, making it a convenient way to isolate small, reusable snippets of markup.markdown
- can be used in two different ways:- as a statement
{{markdown}} <markdown text goes here> {{end}}
, in which case the text inside themarkdown-end
pair is treated as pure markdown (it must not contain Scriptic expressions) - or as a special (pseudo-)function,
{{markdown "mdfilename.md"}}
, in which case the specified external markdown file is pulled in; the markdown file can also contain Scriptic statements and expressions. They are evaluated before the markdown syntax is processed.
- as a statement
The markdown
keyword works only when you provide a markdown text renderer. This is done in a similar way to how the external template loaders are specified:
context.Markdown = new MarkdownRenderer();
where MarkdownRenderer
implements the IStringTransformer
interface:
public interface IStringTransformer
{
string Render(string text);
}
The transformer can do an arbitrary series of transformations on the text, but it was intended to work well with a markdown processor like Markdig
(interestingly, from the same author as Scriban).
- See the Language document for a description of the Scriban language syntax.
- See the Built-in functions document for the list of the Scriban 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 the blog post "Implementing a Text Templating Engine for .NET" by SCriban's author for some behind the scene details.
Scriban has been available for a while as a NuGet package: . Scriptic is not available on NuGet yet.
Scriptic is still very much work-in-progress. It hasn't achieved the same level of stability and maturity as Scriban. That said, Scriptic is just a minor extension of Scriban, so it enjoys a lot of the benefits of its mature code base. We may have inadvertently broken something, though... Anyway, we use Scriptic for production work. YMMV. Use at your own risk.
Scriptic/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
The Scriptic logo, clearly inspired by Scriban's Puzzle, is nevertheless original work by Andrew J. Wozniewicz (source CorelDraw file included).
Scriban was created and is being maintained by Alexandre Mutel aka xoofx. Scriptic was forked from Scriban and extended by Andrew J. Wozniewicz (a.k.a. Ancz).