The Principal Dev – Masterclass for Tech Leads

The Principal Dev – Masterclass for Tech Leads28-29 May

Join

scriban ci Coverage Status NuGet

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.

[!NOTE] 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

Highlights

Features

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

The full documentation is available at https://scriban.github.io.

Installation

Scriban is available as a NuGet package: NuGet

dotnet add package Scriban

The package targets netstandard2.0 and net8.0, so it works with .NET 6+, .NET Framework 4.7.2+, and other compatible runtimes.

Also the Scriban.Signed NuGet package provides signed assemblies.

Source Embedding

The package includes Scriban source files so that you can internalize Scriban into your project instead of consuming it only as a binary dependency. This is useful in environments where NuGet references are not convenient, such as Roslyn source generators.

[!WARNING] Currently, Scriban source files are not marked as read-only in this mode. Do not modify them unless you intend to affect other projects on the same machine that use the embedded sources. Use this feature at your own risk.

In order to activate this feature you need to:

If you are targeting netstandard2.0 or .NET Framework 4.7.2+, you will also need the supporting packages Scriban compiles against. They can already come from another dependency in your project:

<ItemGroup>
  <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
  <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
  <PackageReference Include="PolySharp" Version="1.15.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
  </PackageReference>
</ItemGroup>

[!NOTE] Scriban.targets already defines SCRIBAN_NO_SYSTEM_TEXT_JSON and SCRIBAN_SOURCE_INCLUDE when PackageScribanIncludeSource is true, so you do not need to add these constants manually.

In this mode, all Scriban types are marked as internal.

System.Text.Json-based features are intentionally disabled in source-embedding mode. This includes helpers such as object.from_json, object.to_json, and direct JsonElement import support.

License

This software is released under the BSD-Clause 2 license.

Online Demo

Sponsors

Supports this project with a monthly donation and help me continue improving it. [Become a sponsor]

lilith 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.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.