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:
@@ -141,7 +141,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
const test_includes_exe = b.addExecutable(.{
|
const test_includes_exe = b.addExecutable(.{
|
||||||
.name = "test-includes",
|
.name = "test-includes",
|
||||||
.root_module = b.createModule(.{
|
.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,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
.minimum_zig_version = "0.15.2",
|
.minimum_zig_version = "0.15.2",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.pugz = .{
|
.pugz = .{
|
||||||
.path = "../..",
|
.path = "../../..",
|
||||||
},
|
},
|
||||||
.httpz = .{
|
.httpz = .{
|
||||||
.url = "git+https://github.com/karlseguin/http.zig?ref=master#9ef2ffe8d611ff2e1081e5cf39cb4632c145c5b9",
|
.url = "git+https://github.com/karlseguin/http.zig?ref=master#9ef2ffe8d611ff2e1081e5cf39cb4632c145c5b9",
|
||||||
|
|||||||
@@ -301,6 +301,18 @@ fn about(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
|||||||
res.body = html;
|
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 {
|
fn notFound(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
||||||
res.status = 404;
|
res.status = 404;
|
||||||
|
|
||||||
@@ -389,6 +401,7 @@ pub fn main() !void {
|
|||||||
router.get("/products/:id", productDetail, .{});
|
router.get("/products/:id", productDetail, .{});
|
||||||
router.get("/cart", cart, .{});
|
router.get("/cart", cart, .{});
|
||||||
router.get("/about", about, .{});
|
router.get("/about", about, .{});
|
||||||
|
router.get("/include-demo", includeDemo, .{});
|
||||||
|
|
||||||
// Static files
|
// Static files
|
||||||
router.get("/css/*", serveStatic, .{});
|
router.get("/css/*", serveStatic, .{});
|
||||||
@@ -410,6 +423,7 @@ pub fn main() !void {
|
|||||||
\\ GET /products/:id - Product detail
|
\\ GET /products/:id - Product detail
|
||||||
\\ GET /cart - Shopping cart
|
\\ GET /cart - Shopping cart
|
||||||
\\ GET /about - About page
|
\\ GET /about - About page
|
||||||
|
\\ GET /include-demo - Include directive demo
|
||||||
\\
|
\\
|
||||||
\\ Press Ctrl+C to stop.
|
\\ Press Ctrl+C to stop.
|
||||||
\\
|
\\
|
||||||
|
|||||||
4
src/examples/demo/views/includes/some_partial.pug
Normal file
4
src/examples/demo/views/includes/some_partial.pug
Normal 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.
|
||||||
@@ -47,5 +47,7 @@ block content
|
|||||||
a(href="https://github.com/ankitpatial/pugz") GitHub Repository
|
a(href="https://github.com/ankitpatial/pugz") GitHub Repository
|
||||||
li
|
li
|
||||||
a(href="/products") View Products
|
a(href="/products") View Products
|
||||||
|
li
|
||||||
|
a(href="/include-demo") Include Demo
|
||||||
li
|
li
|
||||||
a(href="/") Back to Home
|
a(href="/") Back to Home
|
||||||
|
|||||||
20
src/examples/demo/views/pages/include-demo.pug
Normal file
20
src/examples/demo/views/pages/include-demo.pug
Normal 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.
|
||||||
@@ -6,6 +6,7 @@ pub fn main() !void {
|
|||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
// Test: Simple include from test_views
|
||||||
var engine = pugz.ViewEngine.init(allocator, .{
|
var engine = pugz.ViewEngine.init(allocator, .{
|
||||||
.views_dir = "test_views",
|
.views_dir = "test_views",
|
||||||
}) catch |err| {
|
}) catch |err| {
|
||||||
@@ -21,4 +22,14 @@ pub fn main() !void {
|
|||||||
defer allocator.free(html);
|
defer allocator.free(html);
|
||||||
|
|
||||||
std.debug.print("=== Rendered HTML ===\n{s}\n=== End ===\n", .{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
3
test_views/home.pug
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
h1 Include Test
|
||||||
|
include includes/some_partial.pug
|
||||||
|
p After include
|
||||||
3
test_views/includes/some_partial.pug
Normal file
3
test_views/includes/some_partial.pug
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.info-box
|
||||||
|
h3 Included Partial
|
||||||
|
p This content comes from includes/some_partial.pug
|
||||||
Reference in New Issue
Block a user