diff --git a/build.zig b/build.zig index 57708d7..ed41815 100644 --- a/build.zig +++ b/build.zig @@ -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 = &.{ diff --git a/src/examples/demo/build.zig.zon b/src/examples/demo/build.zig.zon index 262c9be..f941c08 100644 --- a/src/examples/demo/build.zig.zon +++ b/src/examples/demo/build.zig.zon @@ -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", diff --git a/src/examples/demo/src/main.zig b/src/examples/demo/src/main.zig index 2131fde..e96b6d9 100644 --- a/src/examples/demo/src/main.zig +++ b/src/examples/demo/src/main.zig @@ -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. \\ diff --git a/src/examples/demo/views/includes/some_partial.pug b/src/examples/demo/views/includes/some_partial.pug new file mode 100644 index 0000000..5c11181 --- /dev/null +++ b/src/examples/demo/views/includes/some_partial.pug @@ -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. diff --git a/src/examples/demo/views/pages/about.pug b/src/examples/demo/views/pages/about.pug index f02e18d..9e5d836 100644 --- a/src/examples/demo/views/pages/about.pug +++ b/src/examples/demo/views/pages/about.pug @@ -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 diff --git a/src/examples/demo/views/pages/include-demo.pug b/src/examples/demo/views/pages/include-demo.pug new file mode 100644 index 0000000..ac5626f --- /dev/null +++ b/src/examples/demo/views/pages/include-demo.pug @@ -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. diff --git a/src/tests/test_includes.zig b/src/tests/test_includes.zig index 0bd28c9..6d106db 100644 --- a/src/tests/test_includes.zig +++ b/src/tests/test_includes.zig @@ -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; + } } diff --git a/test_views/home.pug b/test_views/home.pug new file mode 100644 index 0000000..60d8f98 --- /dev/null +++ b/test_views/home.pug @@ -0,0 +1,3 @@ +h1 Include Test +include includes/some_partial.pug +p After include diff --git a/test_views/includes/some_partial.pug b/test_views/includes/some_partial.pug new file mode 100644 index 0000000..a9ed83f --- /dev/null +++ b/test_views/includes/some_partial.pug @@ -0,0 +1,3 @@ +.info-box + h3 Included Partial + p This content comes from includes/some_partial.pug