batiati / mustache-zig Goto Github PK
View Code? Open in Web Editor NEWLogic-less templates for Zig
License: MIT License
Logic-less templates for Zig
License: MIT License
Should be possible to read a template from a UTF-16 or "wide char" source, currently, only 8bits chars are supported.
mustache-zig: stage3
zig: 0.11.0-dev.1636+47e14b7ff
> zig build run
error: _bug...
error: The following command terminated unexpectedly:
const std = @import("std");
const mustache = @import("mustache-zig/src/mustache.zig");
pub const Server = struct {
handler_fns: std.StringHashMap(HandlerFn),
pub const HandlerFn = *const fn(Request) void;
fn handle(self: *Server) !void {
var req = Request{ .path = "/test"};
if (self.handler_fns.get(req.path)) |f| f(req);
}
};
pub const Request = struct {
path: []const u8 = undefined,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator();
var fns = std.StringHashMap(Server.HandlerFn).init(allocator);
try fns.put("/test", rqTest);
defer fns.deinit();
var srv = Server{.handler_fns = fns};
try srv.handle();
}
fn rqTest(req: Request) void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator();
// const Ctx = struct {name: []const u8};
// const ctx: Ctx = .{.name = req.path};
const ctx = .{.name = req.path};
const html = mustache.allocRenderText(allocator, "Hello {{name}} from Zig", ctx) catch return;
defer allocator.free(html);
std.debug.print("{s}\n", .{html});
}
Not sure since I'm new to zig, is this a problem of the zig compiler?
Hi, I'm excited to use this library in my Zig project.
Is there an estimate for how long it would take to implement full Windows support?
I am trying to build the library, in the git root I run zig build
and get the following output
src/ffi/extern_types.zig:51:10: error: extern structs cannot contain fields of type
'?fn(?*const anyopaque, *const ffi.extern_types.Path, *ffi.extern_types.UserData) ca
llconv(.C) ffi.extern_types.PathResolution'
get: ?fn (user_data_handle: UserDataHandle, path: *const Path, out_value: *UserD
ata) callconv(.C) PathResolution,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/ffi/extern_types.zig:51:10: note: only pointer like optionals are extern compati
ble
referenced by:
UserData: src/ffi/extern_types.zig:49:29
UserData: src/ffi/extern_types.zig:51:80
remaining reference traces hidden; use '-freference-trace' to see all reference
traces
src/parsing/parser.zig:140:68: error: expected type '*parsing.text_part.TextPart(.{.
source = .{ .string = .{.copy_strings = true} }, .output = .cache, .features = .{.al
low_redefining_delimiters = true, .preseve_line_breaks_and_indentation = true, .lamb
das = .{ .enabled = .{ ... } }}
Incidently I am trying to also get it working as a package, how can I statically link the library into my zig project ( can they be built a long side one another ? ). Sorry I am new to zig :).
Could you add an example how to use it from zig 0.11 as dynamic link library?
We fail with this test from mustaches spec
- name: Basic Decimal Interpolation
desc: Decimals should interpolate seamlessly with proper significance.
data: { power: 1.210 }
template: '"{{power}} jiggawatts!"'
expected: '"1.21 jiggawatts!"'
Suggestions:
1.21000345698
Implement the optional feature "Inheritance" as described at
https://github.com/mustache/spec/blob/master/specs/~inheritance.yml
Parser errors are very limited, and they have few test cases.
There are some missing features in parser errors:
{{{invalid}}
delimiter
+ {
is recognized as a triple-mustacheThere are a lot of comptime options, some of them still not implemented, poorly named, or not so obvious.
Some options could be visible internally only.
Example:
pub const Features = struct {
allow_redefining_delimiters: bool = true,
preseve_line_breaks_and_indentation: bool = true,
lambdas: Lambdas = .{ .enabled = .{} },
};
Here we can keep track of all comptime options, and the current status:
Option | Meaning | Status |
---|---|---|
ParseTextOptions.copy_strings | Parsed templates should dupe all strings or just take slices from the template source | Implemented |
ParseFileOptions.read_buffer_size | Buffer size of the streamed parser. | Implemented, but it could have a better name |
Features.allow_redefining_delimiters | Allows redefining the delimiters through the tags '{{=' and '=}}' | Not implemented |
Features.preseve_line_breaks_and_indentation | Follow strict mustache's rules about blank spaces and indentation | Implemented, but it could have a better name |
Lambdas.disabled | Disable lambdas support, which means, there is no need to store the tag's content | Implemented |
Lambdas..enabled.max_recursion | When enabled, prevents recursive lambda expansion | Not implemented |
ContextMisses.empty or fail | Behaviour when interpolating an unknown variable | Not implemented, defaults to "empty" always |
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.