Scriban 5.4.6 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET 5.0 .NET Standard 2.0
Install-Package Scriban -Version 5.4.6
dotnet add package Scriban --version 5.4.6
<PackageReference Include="Scriban" Version="5.4.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Scriban --version 5.4.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Scriban, 5.4.6"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Scriban as a Cake Addin
#addin nuget:?package=Scriban&version=5.4.6

// Install Scriban as a Cake Tool
#tool nuget:?package=Scriban&version=5.4.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

scriban Build Status Coverage Status NuGet

<img align="right" width="160px" height="160px" src="img/scriban.png">

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.

// 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 like MyMethodIsNice will be exposed as my_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 a MemberRenamer delegate

New in 3.0+

  • AST is now fully visitable with ScriptVisitor. You can now access Parent on any ScriptNode object and navigate the AST.
    • Improve AST round-trip by preserving whitespaces around template enter{{ and exit}}
  • Several new language features:
    • Hexadecimal/binary numbers: 0x1ef or 0b101010
    • 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.g a?.b?.c)
    • Conditional expressions: cond ? a : b
  • 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 and object.eval_template function to evaluate Scriban expressions/templates at runtime.
  • Better support for IFormattable objects.

Features

  • 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 the Template.ParseLiquid method
    • While the liquid language is less powerful than scriban, this mode allows to migrate from liquid to scriban language easily
    • With the AST to text mode, you can convert a liquid script to a scriban script using Template.ToText on a template parsed with Template.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.
  • Extensible runtime providing many extensibility points
  • Support for async/await evaluation of scripts (e.g Template.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)
  • Complex objects (javascript/json like objects x = {mymember: 1}) and arrays (e.g x = [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

Binaries

Scriban is available as a NuGet package: NuGet

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 to true 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+

Benchmarks

Scriban is blazing fast! For more details, you can check the benchmarks document.

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

Credits

Adapted logo Puzzle by Andrew Doane from the Noun Project

Author

Alexandre Mutel aka xoofx.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (88)

Showing the top 5 NuGet packages that depend on Scriban:

Package Downloads
Serenity.CodeGenerator The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Generates server and script side code for Serenity platform applications

Volo.Abp.TextTemplating.Scriban The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

Miru.Core

Package Description

RecommendationsGatewayModule.Data

Package Description

Volo.Docs.Web The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

GitHub repositories (21)

Showing the top 5 popular GitHub repositories that depend on Scriban:

Repository Stars
abpframework/abp
Open Source Web Application Framework for ASP.NET Core
dodyg/practical-aspnetcore
Practical samples of ASP.NET Core 2.1, 2.2, 3.1, 5.0, 6.0 and 7.0 projects you can use. Readme contains explanations on all projects.
spectreconsole/spectre.console
A .NET library that makes it easier to create beautiful console applications.
serenity-is/Serenity
Business Apps Made Simple with Asp.Net Core MVC / TypeScript
focus-creative-games/luban
你的最佳游戏配置解决方案 {excel, csv, xls, xlsx, json, bson, xml, yaml, lua, unity scriptableobject} => {json, bson, xml, lua, yaml, protobuf(pb), msgpack, flatbuffers, erlang, custom template} data + {c++, java, c#, go(golang), lua, javascript(js), typescript(ts), erlang, rust, gdscript, protobuf schema, flatbuffers schema, custom template} code。 不仅仅是导表工具。支持unity , ue4, tolua, xlua, slua, ilruntime, puerts等热更新插件;强大的数据校验能力;完善的本地化机制。a powerful game config export tool and code generator that supports for i18n and l10n.
Version Downloads Last updated
5.4.6 25,171 5/29/2022
5.4.5 11,683 5/22/2022
5.4.4 82,183 4/9/2022
5.4.3 10,535 4/3/2022
5.4.2 5,563 3/31/2022
5.4.1 38,294 3/15/2022
5.4.0 66,073 2/6/2022
5.3.0 493 2/6/2022
5.2.0 230 2/6/2022
5.1.0 241 2/5/2022
5.0.0 332,304 10/20/2021
4.1.0 18,183 10/7/2021
4.0.2 288,392 9/22/2021
4.0.1 426,768 6/3/2021
4.0.0 2,194 6/2/2021
3.9.0 13,517 5/31/2021
3.8.2 12,276 5/27/2021
3.8.1 6,565 5/25/2021
3.8.0 519 5/25/2021
3.7.0 62,963 5/1/2021
3.6.0 509,522 3/20/2021
3.5.0 155,026 2/13/2021
3.4.2 28,420 2/1/2021
3.4.1 25,605 1/23/2021
3.4.0 11,505 1/21/2021
3.3.3 42,370 1/14/2021
3.3.2 67,307 12/30/2020
3.3.1 48,073 12/30/2020
3.3.0 824 12/29/2020
3.2.2 1,928 12/24/2020
3.2.1 14,133 12/22/2020
3.2.0 611 12/22/2020
3.1.0 4,009 12/21/2020
3.0.7 20,797 12/15/2020
3.0.6 79,702 12/6/2020
3.0.5 190,324 12/1/2020
3.0.4 2,886 11/30/2020
3.0.3 2,058 11/29/2020
3.0.2 43,638 11/23/2020
3.0.1 827 11/22/2020
3.0.0 14,241 11/15/2020
3.0.0-alpha.9 4,295 8/20/2020
3.0.0-alpha.8 316 8/7/2020
3.0.0-alpha.7 196 7/31/2020
3.0.0-alpha.6 218 7/31/2020
3.0.0-alpha.5 441 7/25/2020
3.0.0-alpha.4 269 7/25/2020
3.0.0-alpha.3 4,999 7/16/2020
3.0.0-alpha.2 246 7/6/2020
3.0.0-alpha.1 313 6/18/2020
2.1.4 300,589 8/16/2020
2.1.3 615,705 6/5/2020
2.1.2 222,013 3/8/2020
2.1.1 223,935 12/6/2019
2.1.0 254,236 6/30/2019
2.0.1 348,987 5/11/2019
2.0.0 121,364 3/8/2019
2.0.0-alpha-006 430 3/7/2019
2.0.0-alpha-005 2,030 1/15/2019
2.0.0-alpha-004 1,244 1/4/2019
2.0.0-alpha-003 476 1/3/2019
2.0.0-alpha-002 554 1/1/2019
2.0.0-alpha-001 501 12/28/2018
1.2.9 86,248 12/21/2018
1.2.8 4,776 12/17/2018
1.2.7 134,936 10/6/2018
1.2.6 6,044 9/26/2018
1.2.5 7,329 8/29/2018
1.2.4 2,257 8/21/2018
1.2.3 57,495 7/20/2018
1.2.2 5,001 7/3/2018
1.2.1 25,817 6/1/2018
1.2.0 21,815 2/10/2018
1.1.1 4,920 1/22/2018
1.1.0 843 1/22/2018
1.0.0 39,354 12/24/2017
1.0.0-beta-006 850 12/12/2017
1.0.0-beta-005 675 12/3/2017
1.0.0-beta-004 934 11/19/2017
1.0.0-beta-003 899 11/19/2017
1.0.0-beta-002 898 11/13/2017
1.0.0-beta-001 873 11/12/2017
0.16.0 2,708 11/12/2017
0.15.0 8,760 11/9/2017
0.14.0 1,019 11/7/2017
0.13.0 1,015 11/7/2017
0.12.1 1,015 11/6/2017
0.12.0 996 11/6/2017
0.11.0 1,032 11/4/2017
0.10.0 1,002 11/1/2017
0.9.1 995 10/27/2017
0.9.0 985 10/25/2017
0.9.0-pre100 908 10/23/2017
0.7.0 20,297 10/12/2017
0.6.0 1,321 5/16/2017
0.5.0 808 5/16/2017
0.4.0 807 5/8/2017
0.3.1 841 4/4/2017
0.3.1-pre028 711 4/4/2017
0.3.0 841 3/9/2017
0.2.2 848 2/2/2017
0.2.1 869 2/1/2017
0.2.0 1,579 6/27/2016
0.1.0 1,711 5/31/2016