axispx / zmpl

Zmpl is a templating language written in Zig

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zmpl logo

Zmpl is a templating language for Zig 🦎

  • Use Zig code directly in templates for control flow.
  • Simple and intuitive DSL for building flexible, JSON-compatible data objects.
  • Compiles to Zig code for syntax and type validation.
  • Used by the Jetzig web framework.

Syntax Highlighting

Syntax highlighters are currently community-sourced. Please get in touch if you have created a plugin for your editor of choice and we will gladly list it here.

  • VSCode by Zackary Housend
  • NeoVim syntax highlighting can be achieved by using the above VSCode grammar with the nvim-textmate plugin. You may need to use this fork if you have issues compiling the UTF-8 extension.

Example

See src/templates for more examples.

if (std.mem.eql(u8, "zmpl is simple", "zmpl" ++ " is " ++ "simple")) {
  // Add comments using Zig syntax.
  <div>Email: {.user.email}</div>
  <div>Token: {.auth.token}</div>

  // Render a partial named `users/_mailto.zmpl`:
  <div>{^users/mailto}</div>

  // Pass arguments to a partial:
  <div>{^users/mailto(subject: zmpl.string("Welcome to Jetzig!"))}</div>

  // Pass arguments to a partial with type inference:
  <div>{^users/mailto(subject: "Welcome to Jetzig!")}</div>

  <>Use fragment tags when you want to output content without a specific HTML tag</>

  <#>
  Use multi-line raw text tags to bypass Zmpl syntax.
  <code>Some example code with curly braces {} etc.</code>
  </#>

  <span>Escape curly braces {{like this}}</span>
}
const std = @import("std");
const zmpl = @import("zmpl");
const allocator = std.testing.allocator;
const manifest = @import("zmpl.manifest"); // Generated at build time

test "readme example" {
    var data = zmpl.Data.init(allocator);
    defer data.deinit();

    var body = try data.object();
    var user = try data.object();
    var auth = try data.object();

    try user.put("email", data.string("user@example.com"));
    try auth.put("token", data.string("abc123-456-def"));

    try body.put("user", user);
    try body.put("auth", auth);

    if (manifest.find("example")) |template| {
        const output = try template.render(&data);
        defer allocator.free(output);

        try std.testing.expectEqualStrings(
            \\  <div>Email: user@example.com</div>
            \\  <div>Token: abc123-456-def</div>
            \\
            \\  <div><a href="mailto:user@example.com?subject=">user@example.com</a></div>
            \\
            \\  <div><a href="mailto:user@example.com?subject=Welcome to Jetzig!">user@example.com</a></div>
            \\
            \\  Use fragment tags when you want to output content without a specific HTML tag
            \\
            \\  Use multi-line raw text tags to bypass Zmpl syntax.
            \\  <code>Some example code with curly braces {} etc.</code>
            \\
            \\  <span>Escape curly braces {like this}</span>
            \\
        , output);
    } else {
        try std.testing.expect(false);
    }
}

License

MIT

Credits

Templ - inspiration for template layout.

About

Zmpl is a templating language written in Zig

License:MIT License


Languages

Language:Zig 94.7%Language:Zimpl 5.3%