Giter Site home page Giter Site logo

luis-sousa-pinto / scriban Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scriban/scriban

0.0 0.0 0.0 2.72 MB

A fast, powerful, safe and lightweight scripting language and engine for .NET

License: BSD 2-Clause "Simplified" License

C# 100.00%

scriban's Introduction

scriban Build Status 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.

// 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

Online Demo

Credits

Adapted logo Puzzle by Andrew Doane from the Noun Project

Author

Alexandre Mutel aka xoofx.

scriban's People

Contributors

xoofx avatar neilmacmullen avatar teo-tsirpanis avatar binarycow avatar reima avatar nevespl avatar tatarincev avatar lafar6502 avatar thomaslevesque avatar ebekker avatar workgroupengineering avatar guria avatar jonatthu avatar lahma avatar matteocontrini avatar michael-wolfenden avatar nicholas-brooks avatar 345paul avatar pebezo avatar r3c avatar szyb avatar wimvergouwe avatar xigi avatar badcel avatar francismaio avatar korbul avatar marioantunes avatar totpero avatar marcus-smallman avatar commonguy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.