5.1 KiB
5.1 KiB
Pugz Demo Server
A simple HTTP server demonstrating Pugz template engine with both runtime and compiled template modes.
Quick Start
1. Build Everything
From the pugz root directory (not this demo directory):
cd /path/to/pugz
zig build
This builds:
- The
pugzlibrary - The
pug-compileCLI tool (inzig-out/bin/) - All tests and benchmarks
2. Build Demo Server
cd examples/demo
zig build
3. Run Demo Server
zig build run
The server will start on http://localhost:5882
Using Compiled Templates (Optional)
For maximum performance, you can pre-compile templates to Zig code:
Step 1: Compile Templates
From the pugz root:
./zig-out/bin/pug-compile --dir examples/demo/views --out examples/demo/generated pages
This compiles all .pug files in views/pages/ to Zig functions.
Step 2: Enable Compiled Mode
Edit src/main.zig and set:
const USE_COMPILED_TEMPLATES = true;
Step 3: Rebuild and Run
zig build run
Project Structure
demo/
├── build.zig # Build configuration
├── build.zig.zon # Dependencies
├── src/
│ └── main.zig # Server implementation
├── views/ # Pug templates (runtime mode)
│ ├── layouts/
│ │ └── main.pug
│ ├── partials/
│ │ ├── header.pug
│ │ └── footer.pug
│ └── pages/
│ ├── home.pug
│ └── about.pug
└── generated/ # Compiled templates (after compilation)
├── home.zig
├── about.zig
├── helpers.zig
└── root.zig
Available Routes
GET /- Home pageGET /about- About pageGET /simple- Simple compiled template demo (ifUSE_COMPILED_TEMPLATES=true)
Runtime vs Compiled Templates
Runtime Mode (Default)
- ✅ Full Pug feature support (extends, includes, mixins, loops)
- ✅ Easy development - edit templates and refresh
- ⚠️ Parses templates on every request
Compiled Mode
- ✅ 10-100x faster (no runtime parsing)
- ✅ Type-safe data structures
- ✅ Zero dependencies in generated code
- ⚠️ Limited features (no extends/includes/mixins yet)
- ⚠️ Must recompile after template changes
Development Workflow
Runtime Mode (Recommended for Development)
- Edit
.pugfiles inviews/ - Refresh browser - changes take effect immediately
- No rebuild needed
Compiled Mode (Recommended for Production)
- Edit
.pugfiles inviews/ - Recompile:
../../../zig-out/bin/pug-compile --dir views --out generated pages - Rebuild:
zig build - Restart server
Dependencies
- pugz - Template engine (from parent directory)
- httpz - HTTP server (karlseguin/http.zig)
Troubleshooting
"unable to find module 'pugz'"
Make sure you built from the pugz root directory first:
cd /path/to/pugz # Go to root, not demo/
zig build
"File not found: views/..."
Make sure you're running the server from the demo directory:
cd examples/demo
zig build run
Compiled templates not working
- Verify templates were compiled:
ls -la generated/ - Check
USE_COMPILED_TEMPLATESis set totrueinsrc/main.zig - Rebuild:
zig build
Example: Adding a New Page
Runtime Mode
- Create
views/pages/contact.pug:
extends ../layouts/main.pug
block content
h1 Contact Us
p Email: hello@example.com
- Add route in
src/main.zig:
fn contactPage(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(res.arena, "pages/contact", .{
.siteName = "Demo Site",
}) catch |err| {
return renderError(res, err);
};
res.content_type = .HTML;
res.body = html;
}
// In main(), add route:
server.router().get("/contact", contactPage);
- Restart server and visit
http://localhost:5882/contact
Compiled Mode
- Create simple template (no extends):
views/pages/contact.pug
doctype html
html
head
title Contact
body
h1 Contact Us
p Email: #{email}
-
Compile:
../../../zig-out/bin/pug-compile --dir views --out generated pages -
Add route:
const templates = @import("templates");
fn contactPage(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = try templates.pages_contact.render(res.arena, .{
.email = "hello@example.com",
});
res.content_type = .HTML;
res.body = html;
}
- Rebuild and restart
Performance Tips
- Use compiled templates for production (after development is complete)
- Use ArenaAllocator - Templates are freed all at once after response
- Cache static assets - Serve CSS/JS from CDN or static server
- Keep templates simple - Avoid complex logic in templates