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
Template.ToText
, allowing to manipulate scripts in memory and re-save them to the disk, useful for roundtrip script update scenarios
liquid
by using the Template.ParseLiquid
method
liquid
language is less powerful than scriban, this mode allows to migrate from liquid
to scriban
language easilyliquid
script to a scriban script using Template.ToText
on a template parsed with Template.ParseLiquid
if
/else
/for
/while
, expressions (x = 1 + 2
), conditions... etc.myvar | string.capitalize
)
func
statement and allow function pointers/delegates via the alias @ directive
x = {mymember: 1}
) and arrays (e.g x = [1,2,3,4]
)wrap
statementarrays functions
date
html
maths functions
object
regex functions
string functions
timespan
{{...}}
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 a layout
directive. There can only be one layout
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 corresponding render
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:
{{markdown}} <markdown text goes here> {{end}}
, in which case the text inside the markdown-end
pair is treated as pure markdown (it must not contain Scriptic expressions){{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.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).
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.
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).