Scriban 5.12.1
scriban
Scriban is a fast, powerful, safe and lightweight scripting language and engine for .NET, which was primarily developed for text templating with a compatibility mode for parsing liquid
templates.
Today, not only Scriban can be used in text templating scenarios, but also can be integrated as a general scripting engine: For example, Scriban is at the core of the scripting engine for kalk, a command line calculator application for developers.
// 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 });
Scriban can also be used in pure scripting context without templating ({{
and }}
) and can help you to create your own small DSL.
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
New in 3.0+
- AST is now fully visitable with
ScriptVisitor
. You can now accessParent
on anyScriptNode
object and navigate the AST.- Improve AST round-trip by preserving whitespaces around template enter
{{
and exit}}
- Improve AST round-trip by preserving whitespaces around template enter
- Several new language features:
- Hexadecimal/binary numbers:
0x1ef
or0b101010
- Support for large integers
- New parametric functions:
func sub(x,y = 1, z...); ret x - y - z[0]; end
- New inline functions:
sub(x,y) = x - y
- Optional member access with
?.
instead of regular.
(e.ga?.b?.c
) - Conditional expressions:
cond ? a : b
- Hexadecimal/binary numbers:
- Separate language mode (via
ScriptLang
enum) from template/scripting parsing mode (ScriptMode
). - New language parsing mode
Scientific
, in addition to default Scriban and Liquid language mode. - More fine-grained options on the
TemplateContext
to define scripting behaviors (EnableRelaxedTargetAccess
,EnableRelaxedMemberAccess
,EnableRelaxedFunctionAccess
,EnableRelaxedIndexerAccess
,EnableNullIndexer
) - New
object.eval
andobject.eval_template
function to evaluate Scriban expressions/templates at runtime. - Better support for
IFormattable
objects.
Features
- An extensible sandbox execution model: You have the full control about which Scripting objects (and so properties and methods) are accessible from Scriban templates.
- Very efficient, fast parser and a lightweight runtime. CPU and Garbage Collector friendly.
- 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
Syntax Coloring
You can install the Scriban Extension for Visual Studio Code to get syntax coloring for scriban scripts (without HTML) and scriban html files.
Documentation
- 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.
Binaries
Scriban is available as a NuGet package:
Compatible with the following .NET Standard 2.0+ (New in 3.0)
For support for older framework (.NET 3.5, 4.0, 4.5, .NET Standard 1.1, 1.3, they are only provided in older Scriban 2.x, which is no longer supported.
Also the Scriban.Signed NuGet package provides signed assemblies.
Source Embedding
Starting with Scriban 3.2.1+, the package comes with source included so that you can internalize your usage of Scriban into your project. This can be useful in an environment where you can't easily consume NuGet references (e.g Roslyn Source Generators).
WARNING: Currently, the Scriban sources are not set as readonly, so you should not modify Scriban sources in that mode as it will modify the sources for other projects using Scriban on your machine. Use this feature at your own risks!
In order to activate this feature you need to:
- Set the property
PackageScribanIncludeSource
totrue
in your project:<PropertyGroup> <PackageScribanIncludeSource>true</PackageScribanIncludeSource> </PropertyGroup>
- Add the
IncludeAssets="Build"
to the NuGet PackageReference for Scriban:<ItemGroup> <PackageReference Include="Scriban" Version="3.2.1" IncludeAssets="Build"/> </ItemGroup>
If you are targeting netstandard2.0
or .NET Framework 4.7.2+
, in order to compile Scriban you will need these NuGet package references (that can come from a dependency that you already have):
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.0" />
</ItemGroup>
NOTE: In this mode, all Scriban types are marked as
internal
.You should see a Scriban folder and empty subfolders in your project. This is an issue with Visual Studio 2019 16.8.x (and before) and it will be fixed in VS 2019 16.9+
License
This software is released under the BSD-Clause 2 license.
Related projects
- 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
- Textrude: UI and CLI tools to turn CSV/JSON/YAML models into code using Scriban templates
- NTypewriter: VS extension to turn C# code into documentation/TypeScript/anything using Scriban templates
Online Demo
- (https://scribanonline.azurewebsites.net/): ASP.NET Core Sample.
Sponsors
Supports this project with a monthly donation and help me continue improving it. [Become a sponsor]
Lilith River, author of Imageflow Server, an easy on-demand image editing, optimization, and delivery server
Credits
Adapted logo Puzzle
by Andrew Doane from the Noun Project
Author
Alexandre Mutel aka xoofx.
Showing the top 20 packages that depend on Scriban.
Packages | Downloads |
---|---|
Volo.Abp.TextTemplating
Package Description
|
24 |
Volo.Abp.TextTemplating.Scriban
Package Description
|
22 |
Volo.Abp.TextTemplating.Scriban
Package Description
|
21 |
Volo.Abp.TextTemplating
Package Description
|
21 |
Volo.Abp.TextTemplating
Package Description
|
20 |
Volo.Abp.TextTemplating.Scriban
Package Description
|
20 |
Volo.Abp.TextTemplating.Scriban
Package Description
|
19 |
Volo.Abp.Emailing
Package Description
|
19 |
Volo.Abp.TextTemplating
Package Description
|
19 |
Volo.Abp.TextTemplating.Scriban
Package Description
|
18 |
.NET 7.0
- No dependencies.
.NET Standard 2.0
- Microsoft.CSharp (>= 4.5.0)
- System.Threading.Tasks.Extensions (>= 4.5.0)
Version | Downloads | Last updated |
---|---|---|
5.12.1 | 2 | 12/22/2024 |
5.12.0 | 5 | 11/14/2024 |
5.11.0 | 4 | 11/14/2024 |
5.10.0 | 14 | 05/04/2024 |
5.9.1 | 14 | 02/01/2024 |
5.9.0 | 12 | 12/06/2023 |
5.8.0 | 16 | 11/29/2023 |
5.7.0 | 17 | 06/22/2023 |
5.6.0 | 19 | 10/07/2023 |
5.5.2 | 15 | 09/27/2023 |
5.5.1 | 14 | 06/25/2023 |
5.5.0 | 14 | 07/05/2023 |
5.4.6 | 16 | 07/17/2023 |
5.4.5 | 16 | 07/05/2023 |
5.4.4 | 17 | 06/23/2023 |
5.4.3 | 17 | 10/08/2023 |
5.4.2 | 11 | 10/08/2023 |
5.4.1 | 14 | 10/08/2023 |
5.4.0 | 14 | 07/18/2023 |
5.3.0 | 17 | 10/08/2023 |
5.2.0 | 14 | 10/08/2023 |
5.1.0 | 16 | 07/22/2023 |
5.0.0 | 14 | 06/24/2023 |
4.1.0 | 17 | 08/27/2023 |
4.0.2 | 14 | 07/04/2023 |
4.0.1 | 16 | 10/08/2023 |
4.0.0 | 13 | 08/05/2023 |
3.9.0 | 22 | 06/23/2023 |
3.8.2 | 16 | 08/06/2023 |
3.8.1 | 14 | 10/08/2023 |
3.8.0 | 17 | 07/05/2023 |
3.7.0 | 17 | 10/08/2023 |
3.6.0 | 13 | 10/07/2023 |
3.5.0 | 13 | 07/17/2023 |
3.4.2 | 16 | 08/28/2023 |
3.4.1 | 16 | 10/08/2023 |
3.4.0 | 15 | 10/08/2023 |
3.3.3 | 18 | 07/16/2023 |
3.3.2 | 16 | 07/19/2023 |
3.3.1 | 21 | 10/08/2023 |
3.3.0 | 18 | 10/08/2023 |
3.2.2 | 16 | 06/24/2023 |
3.2.1 | 13 | 10/08/2023 |
3.2.0 | 19 | 10/08/2023 |
3.1.0 | 11 | 03/14/2024 |
3.0.7 | 14 | 07/05/2023 |
3.0.6 | 17 | 07/18/2023 |
3.0.5 | 13 | 10/08/2023 |
3.0.4 | 11 | 10/08/2023 |
3.0.3 | 14 | 10/02/2023 |
3.0.2 | 19 | 06/25/2023 |
3.0.1 | 15 | 08/31/2023 |
3.0.0 | 12 | 10/08/2023 |
3.0.0-alpha.9 | 14 | 01/11/2024 |
3.0.0-alpha.8 | 14 | 01/14/2024 |
3.0.0-alpha.7 | 13 | 01/19/2024 |
3.0.0-alpha.6 | 15 | 01/18/2024 |
3.0.0-alpha.5 | 14 | 01/11/2024 |
3.0.0-alpha.4 | 15 | 01/14/2024 |
3.0.0-alpha.3 | 14 | 02/24/2024 |
3.0.0-alpha.2 | 13 | 01/17/2024 |
3.0.0-alpha.1 | 17 | 01/14/2024 |
2.1.4 | 16 | 10/08/2023 |
2.1.3 | 13 | 12/25/2023 |
2.1.2 | 17 | 10/08/2023 |
2.1.1 | 17 | 10/08/2023 |
2.1.0 | 14 | 10/08/2023 |
2.0.1 | 12 | 10/08/2023 |
2.0.0 | 12 | 10/08/2023 |
2.0.0-alpha-006 | 14 | 10/21/2023 |
2.0.0-alpha-005 | 13 | 10/21/2023 |
2.0.0-alpha-004 | 13 | 10/20/2023 |
2.0.0-alpha-003 | 14 | 10/21/2023 |
2.0.0-alpha-002 | 13 | 10/21/2023 |
2.0.0-alpha-001 | 14 | 10/21/2023 |
1.2.9 | 13 | 10/07/2023 |
1.2.8 | 13 | 10/08/2023 |
1.2.7 | 11 | 10/08/2023 |
1.2.6 | 16 | 10/08/2023 |
1.2.5 | 14 | 10/08/2023 |
1.2.4 | 12 | 10/08/2023 |
1.2.3 | 18 | 10/07/2023 |
1.2.2 | 14 | 10/08/2023 |
1.2.1 | 13 | 10/08/2023 |
1.2.0 | 15 | 10/08/2023 |
1.1.1 | 13 | 10/08/2023 |
1.1.0 | 15 | 10/08/2023 |
1.0.0 | 12 | 10/07/2023 |
1.0.0-beta-006 | 13 | 10/21/2023 |
1.0.0-beta-005 | 15 | 10/21/2023 |
1.0.0-beta-004 | 15 | 10/21/2023 |
1.0.0-beta-003 | 15 | 10/21/2023 |
1.0.0-beta-002 | 14 | 10/21/2023 |
1.0.0-beta-001 | 14 | 10/21/2023 |
0.16.0 | 13 | 10/09/2023 |
0.15.0 | 14 | 10/09/2023 |
0.14.0 | 14 | 10/09/2023 |
0.13.0 | 14 | 10/09/2023 |
0.12.1 | 14 | 10/08/2023 |
0.12.0 | 16 | 10/08/2023 |
0.11.0 | 12 | 10/09/2023 |
0.10.0 | 13 | 10/08/2023 |
0.9.1 | 14 | 10/07/2023 |
0.9.0 | 15 | 10/08/2023 |
0.9.0-pre100 | 12 | 10/26/2023 |
0.7.0 | 13 | 10/08/2023 |
0.6.0 | 15 | 10/08/2023 |
0.5.0 | 15 | 10/08/2023 |
0.4.0 | 12 | 10/07/2023 |
0.3.1 | 15 | 10/07/2023 |
0.3.1-pre028 | 12 | 11/02/2023 |
0.3.0 | 10 | 10/08/2023 |
0.2.2 | 14 | 12/25/2023 |
0.2.1 | 15 | 10/07/2023 |
0.2.0 | 11 | 10/08/2023 |
0.1.0 | 13 | 10/08/2023 |