-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.zig
77 lines (64 loc) · 2.61 KB
/
template.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Example of a basic comptime template.
///This page template is compiled/prepared at comptime.
const ExamplePage = PageData("templates/example.html");
fn index(frame: *verse.Frame) Router.Error!void {
var page = ExamplePage.init(.{
// Simple Variables
.simple_variable = "This is a simple variable",
//.required_but_missing = "Currently unused in the html",
.required_and_provided = "The template requires this from the endpoint",
// Customized Variables
// When ornull is used the default null is provided, as the template
// specifies it can be missing.
//.null_variable = "Intentionally left blank",
// The next var could be deleted as the HTML provides a default
.default_provided = "This is the endpoint provided variable",
// Commented so the HTML provided default can be used.
//.default_missing = "This endpoint var could replaced the default",
.positive_number = 1, // Wanted to write 2, but off by one errors are common
// Logic based Variables.
// A default isn't provided for .optional, because With statements, require
// an explicit decision.
.optional_with = null,
.namespaced_with = .{
.simple_variable = "This is a different variable from above",
},
.basic_loop = &.{
.{ .color = "red", .text = "This color is red" },
.{ .color = "blue", .text = "This color is blue" },
.{ .color = "green", .text = "This color is green" },
},
.slices = &.{
"This is simple ",
"but very useful ",
"for many types of ",
"data generation patterns ",
},
.include_vars = .{
.template_name = "<h1>This is the included html</h1>",
.simple_variable = "This is the simple variable for the included html",
},
// Even if the included html has no variables, the field is still required here
.empty_vars = .{},
});
try frame.sendPage(&page);
}
const routes = Router.Routes(&[_]Router.Match{
Router.GET("", index),
});
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
var server = try verse.Server.init(alloc, routes, .{
.mode = .{ .http = .{ .port = 8082 } },
});
server.serve() catch |err| {
std.debug.print("error: {any}", .{err});
std.posix.exit(1);
};
}
const std = @import("std");
const verse = @import("verse");
const PageData = verse.template.PageData;
const Router = verse.Router;