From e4709e0c7360f106ae32573e5a75795fc0add47d Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Thu, 28 Mar 2024 19:41:45 +0000 Subject: [PATCH 1/2] Replicate exe imports in routes and static routes generator Allow using dependencies added to main exe in views. --- build.zig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/build.zig b/build.zig index 8407f87..890b424 100644 --- a/build.zig +++ b/build.zig @@ -148,6 +148,12 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn exe.root_module.addImport("routes", routes_module); routes_module.addImport("jetzig", jetzig_module); + var it = exe.root_module.import_table.iterator(); + while (it.next()) |import| { + routes_module.addImport(import.key_ptr.*, import.value_ptr.*); + exe_static_routes.root_module.addImport(import.key_ptr.*, import.value_ptr.*); + } + exe_static_routes.root_module.addImport("routes", routes_module); exe_static_routes.root_module.addImport("jetzig", jetzig_module); exe_static_routes.root_module.addImport("zmpl", zmpl_module); From 29d13917a6f6a77d5c072542d866172859330bb4 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Thu, 28 Mar 2024 21:07:16 +0000 Subject: [PATCH 2/2] Add a dependency to demo app Use as a regression test to ensure dependencies always work. --- build.zig | 1 - cli/commands/init.zig | 7 +++++++ demo/build.zig | 6 ++++++ demo/build.zig.zon | 4 ++++ demo/src/app/views/iguanas.zig | 19 +++++++++++++++++-- demo/src/app/views/iguanas/index.zmpl | 5 ++++- 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 890b424..f86691f 100644 --- a/build.zig +++ b/build.zig @@ -146,7 +146,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn }); exe.root_module.addImport("routes", routes_module); - routes_module.addImport("jetzig", jetzig_module); var it = exe.root_module.import_table.iterator(); while (it.next()) |import| { diff --git a/cli/commands/init.zig b/cli/commands/init.zig index 87ace36..a6758b0 100644 --- a/cli/commands/init.zig +++ b/cli/commands/init.zig @@ -191,6 +191,13 @@ pub fn run( github_url, }); + try util.runCommand(allocator, realpath, &[_][]const u8{ + "zig", + "fetch", + "--save", + "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz", + }); + // TODO: Use arg or interactive prompt to do Git setup in net project, default to no. // const git_setup = false; // if (git_setup) try gitSetup(allocator, install_dir); diff --git a/demo/build.zig b/demo/build.zig index ffa5359..8f2e0ae 100644 --- a/demo/build.zig +++ b/demo/build.zig @@ -12,6 +12,12 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }); + // Example dependency: + const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target }); + exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas")); + + // All dependencies **must** be added to imports above this line. + try jetzig.jetzigInit(b, exe, .{}); b.installArtifact(exe); diff --git a/demo/build.zig.zon b/demo/build.zig.zon index 321158c..649531f 100644 --- a/demo/build.zig.zon +++ b/demo/build.zig.zon @@ -6,6 +6,10 @@ .jetzig = .{ .path = "../", }, + .iguanas = .{ + .url = "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz", + .hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c", + }, }, .paths = .{ // This makes *all* files, recursively, included in this package. It is generally diff --git a/demo/src/app/views/iguanas.zig b/demo/src/app/views/iguanas.zig index e96c180..3229685 100644 --- a/demo/src/app/views/iguanas.zig +++ b/demo/src/app/views/iguanas.zig @@ -1,5 +1,6 @@ const std = @import("std"); const jetzig = @import("jetzig"); +const iguanas = @import("iguanas"); /// This example uses a layout. A layout is a template that exists in `src/app/views/layouts` and /// references `{zmpl.content}`. @@ -11,7 +12,21 @@ const jetzig = @import("jetzig"); /// and `demo/src/app/views/iguanas/index.zmpl` pub const layout = "application"; -pub fn index(request: *jetzig.StaticRequest, data: *jetzig.Data) !jetzig.View { - _ = data; +pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { + var root = try data.array(); + + const params = try request.params(); + + const count = if (params.get("iguanas")) |param| + try std.fmt.parseInt(usize, param.string.value, 10) + else + 10; + + const iguanas_slice = try iguanas.iguanas(request.allocator, count); + + for (iguanas_slice) |iguana| { + try root.append(data.string(iguana)); + } + return request.render(.ok); } diff --git a/demo/src/app/views/iguanas/index.zmpl b/demo/src/app/views/iguanas/index.zmpl index 76457d0..7651164 100644 --- a/demo/src/app/views/iguanas/index.zmpl +++ b/demo/src/app/views/iguanas/index.zmpl @@ -1,3 +1,6 @@
- Content goes here + var it = zmpl.value.?.array.iterator(); + while (it.next()) |iguana| { +
{(iguana.string.value)}
+ }