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: about 2 months ago
JSON representation
Logic-less templates for Zig
- Host: GitHub
- URL: https://github.com/batiati/mustache-zig
- Owner: batiati
- License: mit
- Created: 2022-02-15T12:52:35.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-05T19:45:39.000Z (3 months ago)
- Last Synced: 2025-03-29T01:04:07.450Z (2 months ago)
- Topics: mustache, mustache-templating, zig-package, ziglang
- Language: Zig
- Homepage:
- Size: 1.08 MB
- Stars: 137
- Watchers: 2
- Forks: 23
- Open Issues: 11
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MUSTACHE-ZIG
# [{{mustache}}](https://mustache.github.io/) templates for [Zig](https://ziglang.org/).[](https://ziglang.org/)
[](https://github.com/batiati/mustache-zig/actions/workflows/build.yml)
[](https://codecov.io/gh/batiati/mustache-zig)
[](https://github.com/batiati/mustache-zig/blob/master/LICENSE.txt)
# ! 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 `{{&ersant}}`.
✓ 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 fmtThe 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 slower2. 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 slower3. 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 slower4. 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