Compiled temapltes.
Benchmark cleanup
This commit is contained in:
@@ -1,148 +0,0 @@
|
||||
//! Pugz Template Inheritance Demo
|
||||
//!
|
||||
//! A web application demonstrating Pug-style template inheritance
|
||||
//! using the Pugz ViewEngine with http.zig server.
|
||||
//!
|
||||
//! Routes:
|
||||
//! GET / - Home page (layout.pug)
|
||||
//! GET /page-a - Page A with custom scripts and content
|
||||
//! GET /page-b - Page B with sub-layout
|
||||
//! GET /append - Page with block append
|
||||
//! GET /append-opt - Page with optional block syntax
|
||||
|
||||
const std = @import("std");
|
||||
const httpz = @import("httpz");
|
||||
const pugz = @import("pugz");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
/// Application state shared across all requests
|
||||
const App = struct {
|
||||
allocator: Allocator,
|
||||
view: pugz.ViewEngine,
|
||||
|
||||
pub fn init(allocator: Allocator) App {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.view = pugz.ViewEngine.init(.{
|
||||
.views_dir = "src/examples/demo/views",
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer if (gpa.deinit() == .leak) @panic("leak");
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Initialize view engine once at startup
|
||||
var app = App.init(allocator);
|
||||
|
||||
const port = 8080;
|
||||
var server = try httpz.Server(*App).init(allocator, .{ .port = port }, &app);
|
||||
defer server.deinit();
|
||||
|
||||
var router = try server.router(.{});
|
||||
|
||||
// Routes
|
||||
router.get("/", index, .{});
|
||||
router.get("/page-a", pageA, .{});
|
||||
router.get("/page-b", pageB, .{});
|
||||
router.get("/append", pageAppend, .{});
|
||||
router.get("/append-opt", pageAppendOptional, .{});
|
||||
|
||||
std.debug.print(
|
||||
\\
|
||||
\\Pugz Template Inheritance Demo
|
||||
\\==============================
|
||||
\\Server running at http://localhost:{d}
|
||||
\\
|
||||
\\Routes:
|
||||
\\ GET / - Home page (base layout)
|
||||
\\ GET /page-a - Page with custom scripts and content blocks
|
||||
\\ GET /page-b - Page with sub-layout inheritance
|
||||
\\ GET /append - Page with block append
|
||||
\\ GET /append-opt - Page with optional block keyword
|
||||
\\
|
||||
\\Press Ctrl+C to stop.
|
||||
\\
|
||||
, .{port});
|
||||
|
||||
try server.listen();
|
||||
}
|
||||
|
||||
/// Handler for GET /
|
||||
fn index(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||
// Use res.arena - memory is automatically freed after response is sent
|
||||
const html = app.view.render(res.arena, "index", .{
|
||||
.title = "Home",
|
||||
.authenticated = true,
|
||||
}) catch |err| {
|
||||
res.status = 500;
|
||||
res.body = @errorName(err);
|
||||
return;
|
||||
};
|
||||
|
||||
res.content_type = .HTML;
|
||||
res.body = html;
|
||||
}
|
||||
|
||||
/// Handler for GET /page-a - demonstrates extends and block override
|
||||
fn pageA(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||
const html = app.view.render(res.arena, "page-a", .{
|
||||
.title = "Page A - Pets",
|
||||
.items = &[_][]const u8{ "A", "B", "C" },
|
||||
.n = 0,
|
||||
}) catch |err| {
|
||||
res.status = 500;
|
||||
res.body = @errorName(err);
|
||||
return;
|
||||
};
|
||||
|
||||
res.content_type = .HTML;
|
||||
res.body = html;
|
||||
}
|
||||
|
||||
/// Handler for GET /page-b - demonstrates sub-layout inheritance
|
||||
fn pageB(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||
const html = app.view.render(res.arena, "page-b", .{
|
||||
.title = "Page B - Sub Layout",
|
||||
}) catch |err| {
|
||||
res.status = 500;
|
||||
res.body = @errorName(err);
|
||||
return;
|
||||
};
|
||||
|
||||
res.content_type = .HTML;
|
||||
res.body = html;
|
||||
}
|
||||
|
||||
/// Handler for GET /append - demonstrates block append
|
||||
fn pageAppend(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||
const html = app.view.render(res.arena, "page-append", .{
|
||||
.title = "Page Append",
|
||||
}) catch |err| {
|
||||
res.status = 500;
|
||||
res.body = @errorName(err);
|
||||
return;
|
||||
};
|
||||
|
||||
res.content_type = .HTML;
|
||||
res.body = html;
|
||||
}
|
||||
|
||||
/// Handler for GET /append-opt - demonstrates optional block keyword
|
||||
fn pageAppendOptional(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||
const html = app.view.render(res.arena, "page-appen-optional-blk", .{
|
||||
.title = "Page Append Optional",
|
||||
}) catch |err| {
|
||||
res.status = 500;
|
||||
res.body = @errorName(err);
|
||||
return;
|
||||
};
|
||||
|
||||
res.content_type = .HTML;
|
||||
res.body = html;
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
doctype html
|
||||
html
|
||||
head
|
||||
title hello
|
||||
body
|
||||
p some thing
|
||||
| ballah
|
||||
| ballah
|
||||
+btn("click me ", "secondary")
|
||||
br
|
||||
a(href='//google.com' target="_blank") Google 1
|
||||
br
|
||||
a(class='button' href='//google.com' target="_blank") Google 2
|
||||
br
|
||||
a(class='button', href='//google.com' target="_blank") Google 3
|
||||
@@ -1,7 +0,0 @@
|
||||
html
|
||||
head
|
||||
block head
|
||||
script(src='/vendor/jquery.js')
|
||||
script(src='/vendor/caustic.js')
|
||||
body
|
||||
block content
|
||||
@@ -1,10 +0,0 @@
|
||||
html
|
||||
head
|
||||
title My Site - #{title}
|
||||
block scripts
|
||||
script(src='/jquery.js')
|
||||
body
|
||||
block content
|
||||
block foot
|
||||
#footer
|
||||
p some footer content
|
||||
@@ -1,5 +0,0 @@
|
||||
mixin btn(text, type="primary")
|
||||
button(class="btn btn-" + type)= text
|
||||
|
||||
mixin btn-link(href, text)
|
||||
a.btn.btn-link(href=href)= text
|
||||
@@ -1,11 +0,0 @@
|
||||
mixin card(title)
|
||||
.card
|
||||
.card-header
|
||||
h3= title
|
||||
.card-body
|
||||
block
|
||||
|
||||
mixin card-simple(title, body)
|
||||
.card
|
||||
h3= title
|
||||
p= body
|
||||
@@ -1,15 +0,0 @@
|
||||
extends layout.pug
|
||||
|
||||
block scripts
|
||||
script(src='/jquery.js')
|
||||
script(src='/pets.js')
|
||||
|
||||
block content
|
||||
h1= title
|
||||
p Welcome to the pets page!
|
||||
ul
|
||||
li Cat
|
||||
li Dog
|
||||
ul
|
||||
each val in items
|
||||
li= val
|
||||
@@ -1,5 +0,0 @@
|
||||
extends layout
|
||||
|
||||
append head
|
||||
script(src='/vendor/three.js')
|
||||
script(src='/game.js')
|
||||
@@ -1,11 +0,0 @@
|
||||
extends layout-2.pug
|
||||
|
||||
block append head
|
||||
script(src='/vendor/three.js')
|
||||
script(src='/game.js')
|
||||
|
||||
block content
|
||||
p
|
||||
| cheks manually the head section
|
||||
br
|
||||
| hello there
|
||||
@@ -1,9 +0,0 @@
|
||||
extends sub-layout.pug
|
||||
|
||||
block content
|
||||
.sidebar
|
||||
block sidebar
|
||||
p nothing
|
||||
.primary
|
||||
block primary
|
||||
p nothing
|
||||
@@ -1 +0,0 @@
|
||||
p= petName
|
||||
@@ -1,9 +0,0 @@
|
||||
extends layout.pug
|
||||
|
||||
block content
|
||||
.sidebar
|
||||
block sidebar
|
||||
p nothing
|
||||
.primary
|
||||
block primary
|
||||
p nothing
|
||||
Reference in New Issue
Block a user