feat: add include demo page to showcase include directive

- Add includes/some_partial.pug partial in demo views
- Add pages/include-demo.pug page using include directive
- Add /include-demo route in demo server
- Add link to include demo in about page
- Fix test_includes.zig path in build.zig
- Add test_views for include testing
This commit is contained in:
2026-01-25 16:35:27 +05:30
parent 7bcb79c7bc
commit 9d3b729c6c
9 changed files with 59 additions and 2 deletions

View File

@@ -141,7 +141,7 @@ pub fn build(b: *std.Build) void {
const test_includes_exe = b.addExecutable(.{
.name = "test-includes",
.root_module = b.createModule(.{
.root_source_file = b.path("examples/test_includes.zig"),
.root_source_file = b.path("src/tests/test_includes.zig"),
.target = target,
.optimize = optimize,
.imports = &.{

View File

@@ -5,7 +5,7 @@
.minimum_zig_version = "0.15.2",
.dependencies = .{
.pugz = .{
.path = "../..",
.path = "../../..",
},
.httpz = .{
.url = "git+https://github.com/karlseguin/http.zig?ref=master#9ef2ffe8d611ff2e1081e5cf39cb4632c145c5b9",

View File

@@ -301,6 +301,18 @@ fn about(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
res.body = html;
}
fn includeDemo(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(res.arena, "pages/include-demo", .{
.title = "Include Demo",
.cartCount = "2",
}) catch |err| {
return renderError(res, err);
};
res.content_type = .HTML;
res.body = html;
}
fn notFound(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
res.status = 404;
@@ -389,6 +401,7 @@ pub fn main() !void {
router.get("/products/:id", productDetail, .{});
router.get("/cart", cart, .{});
router.get("/about", about, .{});
router.get("/include-demo", includeDemo, .{});
// Static files
router.get("/css/*", serveStatic, .{});
@@ -410,6 +423,7 @@ pub fn main() !void {
\\ GET /products/:id - Product detail
\\ GET /cart - Shopping cart
\\ GET /about - About page
\\ GET /include-demo - Include directive demo
\\
\\ Press Ctrl+C to stop.
\\

View File

@@ -0,0 +1,4 @@
.info-box
h3 Included Partial
p This content comes from includes/some_partial.pug
p It demonstrates the include directive for reusable template fragments.

View File

@@ -47,5 +47,7 @@ block content
a(href="https://github.com/ankitpatial/pugz") GitHub Repository
li
a(href="/products") View Products
li
a(href="/include-demo") Include Demo
li
a(href="/") Back to Home

View File

@@ -0,0 +1,20 @@
extends layouts/base.pug
block title
title Include Demo | Pugz Store
block content
section.page-header
.container
h1 Include Demo
p Demonstrating the include directive
section.section
.container
h2 Content from this page
p The box below is included from a separate partial file.
include includes/some_partial.pug
h2 After the include
p This content comes after the included partial.

View File

@@ -6,6 +6,7 @@ pub fn main() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Test: Simple include from test_views
var engine = pugz.ViewEngine.init(allocator, .{
.views_dir = "test_views",
}) catch |err| {
@@ -21,4 +22,14 @@ pub fn main() !void {
defer allocator.free(html);
std.debug.print("=== Rendered HTML ===\n{s}\n=== End ===\n", .{html});
// Verify output contains included content
if (std.mem.indexOf(u8, html, "Included Partial") != null and
std.mem.indexOf(u8, html, "info-box") != null)
{
std.debug.print("\nSUCCESS: Include directive works correctly!\n", .{});
} else {
std.debug.print("\nFAILURE: Include content not found!\n", .{});
return error.TestFailed;
}
}

3
test_views/home.pug Normal file
View File

@@ -0,0 +1,3 @@
h1 Include Test
include includes/some_partial.pug
p After include

View File

@@ -0,0 +1,3 @@
.info-box
h3 Included Partial
p This content comes from includes/some_partial.pug