
_Zmpl_ is a templating language for [Zig](https://ziglang.org/) :lizard:
* 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](https://github.com/jetzig-framework/jetzig) web framework.
## Documentation
Visit the [Jetzig Documentation](https://jetzig.dev/documentation.html) page to see detailed _Zmpl_ documentation with usage examples.
## Syntax Highlighting
* [Vim](https://github.com/jetzig-framework/zmpl.vim)
* [VSCode](https://github.com/z1fire/zmpl-syntax-highlighting-vscode) by [Zackary Housend](https://github.com/z1fire)
* [VSCode extension](https://marketplace.visualstudio.com/items?itemName=uzyn.zmpl) by [U-Zyn Chua](https://github.com/uzyn)
* [Sublime Text package](https://packagecontrol.io/packages/Zmpl) by [U-Zyn Chua](https://github.com/uzyn)
## Example
See [src/templates](src/templates) for more examples.
### Template
```zig
@zig {
if (std.mem.eql(u8, "zmpl is simple", "zmpl" ++ " is " ++ "simple")) {
Zmpl is simple!
}
}
Email: {{$.user.email}}
Token: {{$.auth.token}}
@partial example_partial
@partial mailto($.user.email, "Welcome to Jetzig!")
@partial mailto(email: $.user.email, subject: "Welcome to Jetzig!")
@partial mailto(email: $.user.email, subject: "Welcome to Jetzig!") {
Sign in
Unsubscribe
}
@markdown {
# Built-in markdown support
* [jetzig.dev](https://www.jetzig.dev/)
}
```
### `mailto` Partial
```zig
@args email: *ZmplValue, subject: []const u8
{{email}}
@zig {
for (slots, 0..) |slot, slot_index| {
{{slot}}
}
}
```
### Output HTML
```html
Zmpl is simple!
Email: user@example.com
Token: abc123-456-def
An example partial
user@example.com
user@example.com
user@example.com
Built-in markdown support
```
### Example Usage
Default template path is `src/templates`. Use `-Dzmpl_templates_path=...` to set an alternative (relative or absolute) path.
`render` receives four arguments:
* `data`: A pointer to a `zmpl.Data`, used by references to look up template data.
* `Context`: A type defining some predefined data that is passed to every template. `Context` **must** be the same type passed to every template, but its structure is arbitrary.
* `context`: A value of type `Context` containing specific values. Available as `context` within every template.
* `options`: Template render options:
```zig
pub const RenderOptions = struct {
/// Specify a layout to wrap the rendered content within. In the template layout, use
/// `{{zmpl.content}}` to render the inner content.
layout: ?Manifest.Template = null,
};
```
```zig
const std = @import("std");
const zmpl = @import("zmpl");
const Context = struct { foo: []const u8 = "default" };
const context = Context{ .foo = "bar" };
test "readme example" {
var data = zmpl.Data.init(std.testing.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 (zmpl.find("example")) |template| {
const output = try template.render(&data, Context, context, .{});
defer std.testing.allocator.free(output);
try std.testing.expectEqualStrings(
\\
\\ Zmpl is simple!
\\
\\
\\
\\Email: user@example.com
\\Token: abc123-456-def
\\
\\
\\An example partial
\\
\\user@example.com
\\
\\user@example.com
\\
\\user@example.com
\\
\\
\\
\\Built-in markdown support
\\
, output);
} else {
try std.testing.expect(false);
}
}
```
## License
[MIT](LICENSE)
## Credits
[Templ](https://github.com/a-h/templ) - inspiration for template layout.