Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/batiati/mustache-zig

Logic-less templates for Zig
https://github.com/batiati/mustache-zig

mustache mustache-templating zig-package ziglang

Last synced: 9 days ago
JSON representation

Logic-less templates for Zig

Awesome Lists containing this project

README

        

# MUSTACHE-ZIG
# [{{mustache}}](https://mustache.github.io/) templates for [Zig](https://ziglang.org/).

[![made with Zig](https://img.shields.io/badge/made%20with%20%E2%9D%A4%20-Zig-orange)](https://ziglang.org/)
[![Matrix Build](https://github.com/batiati/mustache-zig/actions/workflows/build.yml/badge.svg)](https://github.com/batiati/mustache-zig/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/batiati/mustache-zig/branch/master/graph/badge.svg)](https://codecov.io/gh/batiati/mustache-zig)
[![license mit](https://img.shields.io/github/license/batiati/mustache-zig)](https://github.com/batiati/mustache-zig/blob/master/LICENSE.txt)

![logo](mustache.png)

# ! Under development !

## [Read more on Zig News](https://zig.news/batiati/growing-a-mustache-with-zig-di4)

#
## Features

✓ [Comments](https://github.com/mustache/spec/blob/master/specs/comments.yml) `{{! Mustache is awesome }}`.

✓ Custom [delimiters](https://github.com/mustache/spec/blob/master/specs/delimiters.yml) `{{=[ ]=}}`.

✓ [Interpolation](https://github.com/mustache/spec/blob/master/specs/interpolation.yml) of common types, such as strings, enums, bools, optionals, pointers, integers, floats and JSON objects into `{{variables}`.

✓ [Unescaped interpolation](https://github.com/mustache/spec/blob/b2aeb3c283de931a7004b5f7a2cb394b89382369/specs/interpolation.yml#L52) with `{{{tripple-mustache}}}` or `{{&ampersant}}`.

✓ Rendering [sections](https://github.com/mustache/spec/blob/master/specs/sections.yml) `{{#foo}} ... {{/foo}}`.

✓ [Section iterator](https://github.com/mustache/spec/blob/b2aeb3c283de931a7004b5f7a2cb394b89382369/specs/sections.yml#L133) over slices, arrays and tuples `{{slice}} ... {{/slice}}`.

✓ Rendering [inverted sections](https://github.com/mustache/spec/blob/master/specs/inverted.yml) `{{^foo}} ... {{/foo}}`.

✓ [Lambdas](https://github.com/mustache/spec/blob/master/specs/~lambdas.yml) expansion.

✓ Rendering [partials](https://github.com/mustache/spec/blob/master/specs/partials.yml) `{{>file.html}}`.

☐ Rendering [parents and blocks](https://github.com/mustache/spec/blob/master/specs/~inheritance.yml) `{{For comparision with mustache-zig, refer to "Rendering to a new allocated string 1 million times" and "Parsing a template 1 million times" sections bellow.
### Mustache vs Zig's fmt

The same benchmark was implemented in Zig for both mustache-zig and Zig's `std.fmt`.

We can assume that Zig's `std.fmt` is the **fastest** possible way to render a simple string using Zig. [This benchmark](benchmark/src/ramhorns_bench.zig) shows how much **slower** a mustache template is rendered when compared with the same template rendered by Zig's `std.fmt`.

1. Rendering to a pre-allocated buffer 1 million times

| | Total time | ns/iter | MB/s | Penality
----------------|------------|---------|-----------|-------
|Zig fmt | 0.042s | 42 ns | 2596 MB/s | --
|mustache-zig | 0.094s | 94 ns | 1149 MB/s | 2.260x slower

2. Rendering to a new allocated string 1 million times

| | Total time | ns/iter | MB/s | Penality
----------------|------------|---------|-----------|-------
|Zig fmt | 0.058s | 58 ns | 1869 MB/s | --
|mustache-zig | 0.167s | 167 ns | 645 MB/s | 2.897x slower

3. Rendering to a local file 1 million times

| | Total time | ns/iter | MB/s | Penality
----------------|------------|---------|-----------|-------
|Zig fmt | 0.079s | 79 ns | 1367 MB/s | --
|mustache-zig | 0.125s | 125 ns | 862 MB/s | 1.586x slower

4. Parsing a template 1 million times

| | Total time | ns/iter | MB/s
----------------|------------|----------|-----------
|mustache-zig | 1.380s | 1,380 ns | 182 MB/s

_*All benchmarks were compiled as ReleaseSafe, and executed on a Intel i7-1185G7 @ 3.00GHz, Linux kernel 5.17_

### Memory benchmarks

Mustache templates are well known for HTML templating, but it's useful to render any kind of dynamic document, and potentially load templates from untrusted or user-defined sources.

So, it's also important to be able to deal with multi-megabyte inputs without eating all your RAM.

```Zig

// 32KB should be enough memory for this job
// 16KB if we don't need to support lambdas 😅
var plenty_of_memory = std.heap.GeneralPurposeAllocator(.{ .enable_memory_limit = true }){
.requested_memory_limit = 32 * 1024,
};
defer _ = plenty_of_memory.deinit();

try mustache.renderFile(plenty_of_memory.allocator(), "10MB_file.mustache", ctx, out_writer);

```

## Licensing

- MIT

- Mustache is Copyright (C) 2009 Chris Wanstrath
Original CTemplate by Google