Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hamedfathi/dotliquidextended
A library to extend the DotLiquid functionalities.
https://github.com/hamedfathi/dotliquidextended
csharp dotliquid dotliquid-template dotnet template-engine template-engines templates templete
Last synced: 1 day ago
JSON representation
A library to extend the DotLiquid functionalities.
- Host: GitHub
- URL: https://github.com/hamedfathi/dotliquidextended
- Owner: HamedFathi
- License: mit
- Created: 2021-11-08T22:40:11.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-17T18:16:32.000Z (8 months ago)
- Last Synced: 2024-10-11T18:13:37.582Z (about 1 month ago)
- Topics: csharp, dotliquid, dotliquid-template, dotnet, template-engine, template-engines, templates, templete
- Language: C#
- Homepage:
- Size: 33.2 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> [!IMPORTANT]
> Introducing **HamedStack**! For all my latest libraries, visit: [Explore HamedStack](https://github.com/HamedStack). Replacements and updates are available at the link. Thank you for your support! The new version of this library is accessible via [HamedStack.DotLiquid](https://github.com/HamedStack/HamedStack.DotLiquid)
---![logo_big](https://user-images.githubusercontent.com/8418700/140829550-9b90ffc0-d13e-48c3-b2bc-ff84827b0adf.png)
> DotLiquid is a templating system ported to the .net framework from Ruby’s [Liquid Markup](https://shopify.github.io/liquid/).
### [Nuget](https://www.nuget.org/packages/DotLiquidExtended)
[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://opensource.org/licenses/MIT)
![Nuget](https://img.shields.io/nuget/v/DotLiquidExtended)
![Nuget](https://img.shields.io/nuget/dt/DotLiquidExtended)```
Install-Package DotLiquidExtendeddotnet add package DotLiquidExtended
```
### In-Memory File System
By default `dotLiquid` just reads template files from disk via `include` functionality. `InMemoryFileSystem` helps you to use `include` method to load your templates from memory.
```cs
// In-memory templates
public static class Templates
{
public static string GetTemplate1()
{
// You can use single qoutes too.
// {% include 'tmpl2' %}
return @"Hello, {{name}} {% include ""tmpl2"" %}.";
}public static string GetTemplate2()
{
return @"{{family}}";
}
}// Template engine
using DotLiquidExtended;Template.FileSystem = new InMemoryFileSystem(new()
{
// Key, Value
{ "tmpl1", Templates.GetTemplate1() },
{ "tmpl2", Templates.GetTemplate2() }
});
Template template = Template.Parse(Templates.GetTemplate1());
var result = template.Render(Hash.FromAnonymousObject(
new { Name = "Hamed", Family = "Fathi" }
));
```![pic1](https://user-images.githubusercontent.com/8418700/145047229-04dac9cb-6f48-4f61-9f36-a876dc233386.png)
### Extensions
There are bunch of useful extensions as following:
```cs
using DotLiquidExtended;// A simple way to set an anonymous object.
// new { YourObjectName = YourObject }
string RenderAnonymousObject(this Template template, object obj, bool inclueBaseClassProperties = false, IFormatProvider formatProvider = null)// It sets 'RootObject' for your anonymous object.
// new { RootObject = obj }
// You have access to it inside a template via 'root_object' with default naming convention.
RenderObject(this Template template, object obj, bool inclueBaseClassProperties = false, IFormatProvider formatProvider = null)// Returns all AST nodes recursively.
// e.g. template.GetAllNodes().Where(node => node is Variable) returns all variables.
IEnumerable GetAllNodes(this Template template)
````### Utilities
To access below utilities you should call `DotLiquidUtility` static class.
```cs
using DotLiquidExtended;// You can register your types into the template engine.
void RegisterSafeTypes(params Type[] types)// Automatically registers all types and properties of entry assembly.
// You can register all types of referenced assseblies too.
void RegisterSafeTypes(bool withReferencedAssemblies = false)// Automatically registers all types and properties of an specified assembly.
void RegisterSafeTypes(Assembly assembly)// Automatically registers all types and properties of all introduced assembelies.
void RegisterSafeTypes(IEnumerable assemblies)
```##### Render with Variable Validation
There is a `RenderWithValidation` method inside `DotLiquidUtility` to help you detect a variable with two below conditions:
1. Variable is `null`.
2. Variable does not exist.```cs
RenderResult RenderWithValidation(string templateText, object data, Func, bool> ignoreValidationCondition = null)
```In fact, this method simulate a strict variable/property checking because if `dotLiquid` is not able to find a variable just ignores it and renders nothing without any warning or error.
* `templateText`: You should pass your template a text.
* `data`: is your anonymous object to fill template. `new { YourObjectName = YourObject }`
* `ignoreValidationCondition` is a functionality that help you to ignore some variables or filters for checking.```cs
// Func, bool> ignoreValidationCondition// Filters are related to the current variable.
(varName, filters) => {
if(varName.Contains('name')) return true;
if(filters != null && filters.Contains('nullable')) return true;
return false;
}
```Obviously, The entire variables with the exact `name` word in their names will ignore from checking same as all variables with `nullable` filter. e.g. `myVar | nullable`
By default you can use a pre-defined `ignore_safe_var`
filter to skip this checking.In the end you will see a result similar to the following class.
```cs
public class RenderResult
{
// dotLiquid template object.
public Template Template { get; set; }
// Contains whole errors.
// It will be null if there is no error.
public IEnumerable Errors { get; set; }
// The final rendered result.
// It will be null if you have an error.
public string Result { get; set; }
}
```![pic2](https://user-images.githubusercontent.com/8418700/145047276-28c57fe3-b3c6-42d3-add1-fcc2feb3fa42.png)
### `Liquid` related works
* [Fluid](https://github.com/sebastienros/fluid)
* [Scriban](https://github.com/scriban/scriban)
* [LiquidJS](https://liquidjs.com/)
* [ACE Editor](https://ace.c9.io/build/kitchen-sink.html) Change `Document` and `Mode` to `Liquid`.