mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-15 06:26:07 +00:00
Inject jetzig_view
and jetzig_action
into Zmpl templates
This commit is contained in:
parent
bf62fdcf5b
commit
f8d1db5460
23
build.zig
23
build.zig
@ -34,18 +34,33 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.zmpl_templates_path = template_path,
|
.zmpl_templates_path = template_path,
|
||||||
|
.zmpl_auto_build = false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
lib.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
const zmpl_module = zmpl_dep.module("zmpl");
|
||||||
jetzig_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
|
||||||
jetzig_module.addImport("args", zig_args_dep.module("args"));
|
|
||||||
|
|
||||||
// This is the way to make it look nice in the zig build script
|
// This is the way to make it look nice in the zig build script
|
||||||
// If we would do it the other way around, we would have to do
|
// If we would do it the other way around, we would have to do
|
||||||
// b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl");
|
// b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl");
|
||||||
b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory");
|
b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory");
|
||||||
|
|
||||||
|
const ZmplBuild = @import("zmpl").ZmplBuild;
|
||||||
|
const ZmplTemplate = @import("zmpl").Template;
|
||||||
|
var zmpl_build = ZmplBuild.init(b, lib, template_path);
|
||||||
|
const TemplateConstants = struct {
|
||||||
|
jetzig_view: []const u8,
|
||||||
|
jetzig_action: []const u8,
|
||||||
|
};
|
||||||
|
const ZmplOptions = struct {
|
||||||
|
pub const template_constants = TemplateConstants;
|
||||||
|
};
|
||||||
|
const manifest_module = try zmpl_build.compile(ZmplTemplate, ZmplOptions);
|
||||||
|
zmpl_module.addImport("zmpl.manifest", manifest_module);
|
||||||
|
|
||||||
|
lib.root_module.addImport("zmpl", zmpl_module);
|
||||||
|
jetzig_module.addImport("zmpl", zmpl_module);
|
||||||
|
jetzig_module.addImport("args", zig_args_dep.module("args"));
|
||||||
|
|
||||||
const main_tests = b.addTest(.{
|
const main_tests = b.addTest(.{
|
||||||
.root_source_file = .{ .path = "src/tests.zig" },
|
.root_source_file = .{ .path = "src/tests.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.zmpl = .{
|
.zmpl = .{
|
||||||
.url = "https://github.com/jetzig-framework/zmpl/archive/04df14930ea484a9c900c53da9fa4fed3c831111.tar.gz",
|
.url = "https://github.com/jetzig-framework/zmpl/archive/9e2df115c9f17e92fb60a4d09bf55ea48d0388b0.tar.gz",
|
||||||
.hash = "1220e5ede084ca6b94defd466a8f8779aab151d37bf688fefb928fded6f02cde4135",
|
.hash = "1220820b7f5f3e01b7dc976d32cf9ff65d44dee2642533f4b8104e19a824e802d7e1",
|
||||||
},
|
},
|
||||||
.args = .{
|
.args = .{
|
||||||
.url = "https://github.com/MasterQ32/zig-args/archive/01d72b9a0128c474aeeb9019edd48605fa6d95f7.tar.gz",
|
.url = "https://github.com/MasterQ32/zig-args/archive/01d72b9a0128c474aeeb9019edd48605fa6d95f7.tar.gz",
|
||||||
|
@ -13,6 +13,7 @@ const Self = @This();
|
|||||||
|
|
||||||
const Function = struct {
|
const Function = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
|
view_name: []const u8,
|
||||||
args: []Arg,
|
args: []Arg,
|
||||||
path: []const u8,
|
path: []const u8,
|
||||||
source: []const u8,
|
source: []const u8,
|
||||||
@ -163,6 +164,7 @@ fn writeRoute(self: *Self, writer: std.ArrayList(u8).Writer, route: Function) !v
|
|||||||
\\ .{{
|
\\ .{{
|
||||||
\\ .name = "{s}",
|
\\ .name = "{s}",
|
||||||
\\ .action = "{s}",
|
\\ .action = "{s}",
|
||||||
|
\\ .view_name = "{s}",
|
||||||
\\ .uri_path = "{s}",
|
\\ .uri_path = "{s}",
|
||||||
\\ .template = "{s}",
|
\\ .template = "{s}",
|
||||||
\\ .module = @import("{s}"),
|
\\ .module = @import("{s}"),
|
||||||
@ -189,11 +191,16 @@ fn writeRoute(self: *Self, writer: std.ArrayList(u8).Writer, route: Function) !v
|
|||||||
|
|
||||||
const module_path = try self.allocator.dupe(u8, route.path);
|
const module_path = try self.allocator.dupe(u8, route.path);
|
||||||
defer self.allocator.free(module_path);
|
defer self.allocator.free(module_path);
|
||||||
|
|
||||||
|
const view_name = try self.allocator.dupe(u8, route.view_name);
|
||||||
|
defer self.allocator.free(view_name);
|
||||||
|
|
||||||
std.mem.replaceScalar(u8, module_path, '\\', '/');
|
std.mem.replaceScalar(u8, module_path, '\\', '/');
|
||||||
|
|
||||||
const output = try std.fmt.allocPrint(self.allocator, output_template, .{
|
const output = try std.fmt.allocPrint(self.allocator, output_template, .{
|
||||||
full_name,
|
full_name,
|
||||||
route.name,
|
route.name,
|
||||||
|
route.view_name,
|
||||||
uri_path,
|
uri_path,
|
||||||
full_name,
|
full_name,
|
||||||
module_path,
|
module_path,
|
||||||
@ -443,8 +450,11 @@ fn parseFunction(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const view_name = path[0 .. path.len - std.fs.path.extension(path).len];
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.name = function_name,
|
.name = function_name,
|
||||||
|
.view_name = try self.allocator.dupe(u8, view_name),
|
||||||
.path = try std.fs.path.join(self.allocator, &[_][]const u8{ "src", "app", "views", path }),
|
.path = try std.fs.path.join(self.allocator, &[_][]const u8{ "src", "app", "views", path }),
|
||||||
.args = try self.allocator.dupe(Arg, args.items),
|
.args = try self.allocator.dupe(Arg, args.items),
|
||||||
.source = try self.allocator.dupe(u8, source),
|
.source = try self.allocator.dupe(u8, source),
|
||||||
|
@ -40,6 +40,7 @@ fn compileStaticRoutes(allocator: std.mem.Allocator) !void {
|
|||||||
const route = jetzig.views.Route{
|
const route = jetzig.views.Route{
|
||||||
.name = static_route.name,
|
.name = static_route.name,
|
||||||
.action = @field(jetzig.views.Route.Action, static_route.action),
|
.action = @field(jetzig.views.Route.Action, static_route.action),
|
||||||
|
.view_name = static_route.view_name,
|
||||||
.view = static_view,
|
.view = static_view,
|
||||||
.static = true,
|
.static = true,
|
||||||
.uri_path = static_route.uri_path,
|
.uri_path = static_route.uri_path,
|
||||||
@ -104,6 +105,9 @@ fn writeContent(
|
|||||||
var content: []const u8 = undefined;
|
var content: []const u8 = undefined;
|
||||||
defer allocator.free(content);
|
defer allocator.free(content);
|
||||||
|
|
||||||
|
try view.data.addConst("jetzig_view", view.data.string(route.name));
|
||||||
|
try view.data.addConst("jetzig_action", view.data.string(@tagName(route.action)));
|
||||||
|
|
||||||
if (route.layout) |layout_name| {
|
if (route.layout) |layout_name| {
|
||||||
// TODO: Allow user to configure layouts directory other than src/app/views/layouts/
|
// TODO: Allow user to configure layouts directory other than src/app/views/layouts/
|
||||||
const prefixed_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "layouts_", layout_name });
|
const prefixed_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "layouts_", layout_name });
|
||||||
|
@ -126,6 +126,7 @@ pub fn route(comptime routes: anytype) []views.Route {
|
|||||||
detected[index] = .{
|
detected[index] = .{
|
||||||
.name = dynamic_route.name,
|
.name = dynamic_route.name,
|
||||||
.action = @field(views.Route.Action, dynamic_route.action),
|
.action = @field(views.Route.Action, dynamic_route.action),
|
||||||
|
.view_name = dynamic_route.view_name,
|
||||||
.view = view,
|
.view = view,
|
||||||
.static = false,
|
.static = false,
|
||||||
.uri_path = dynamic_route.uri_path,
|
.uri_path = dynamic_route.uri_path,
|
||||||
@ -158,6 +159,7 @@ pub fn route(comptime routes: anytype) []views.Route {
|
|||||||
detected[index] = .{
|
detected[index] = .{
|
||||||
.name = static_route.name,
|
.name = static_route.name,
|
||||||
.action = @field(views.Route.Action, static_route.action),
|
.action = @field(views.Route.Action, static_route.action),
|
||||||
|
.view_name = static_route.view_name,
|
||||||
.view = view,
|
.view = view,
|
||||||
.static = true,
|
.static = true,
|
||||||
.uri_path = static_route.uri_path,
|
.uri_path = static_route.uri_path,
|
||||||
|
@ -29,6 +29,7 @@ pub fn start(self: Self, comptime_routes: []jetzig.views.Route) !void {
|
|||||||
route.* = jetzig.views.Route{
|
route.* = jetzig.views.Route{
|
||||||
.name = comptime_route.name,
|
.name = comptime_route.name,
|
||||||
.action = comptime_route.action,
|
.action = comptime_route.action,
|
||||||
|
.view_name = comptime_route.view_name,
|
||||||
.uri_path = comptime_route.uri_path,
|
.uri_path = comptime_route.uri_path,
|
||||||
.view = comptime_route.view,
|
.view = comptime_route.view,
|
||||||
.static_view = comptime_route.static_view,
|
.static_view = comptime_route.static_view,
|
||||||
|
@ -204,6 +204,7 @@ fn renderView(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (request.rendered_multiple) return error.JetzigMultipleRenderError;
|
if (request.rendered_multiple) return error.JetzigMultipleRenderError;
|
||||||
|
|
||||||
if (request.rendered_view) |rendered_view| {
|
if (request.rendered_view) |rendered_view| {
|
||||||
if (request.redirected) return .{ .view = rendered_view, .content = "" };
|
if (request.redirected) return .{ .view = rendered_view, .content = "" };
|
||||||
|
|
||||||
@ -233,6 +234,9 @@ fn renderTemplateWithLayout(
|
|||||||
view: jetzig.views.View,
|
view: jetzig.views.View,
|
||||||
route: *jetzig.views.Route,
|
route: *jetzig.views.Route,
|
||||||
) ![]const u8 {
|
) ![]const u8 {
|
||||||
|
try view.data.addConst("jetzig_view", view.data.string(route.view_name));
|
||||||
|
try view.data.addConst("jetzig_action", view.data.string(@tagName(route.action)));
|
||||||
|
|
||||||
if (request.getLayout(route)) |layout_name| {
|
if (request.getLayout(route)) |layout_name| {
|
||||||
// TODO: Allow user to configure layouts directory other than src/app/views/layouts/
|
// TODO: Allow user to configure layouts directory other than src/app/views/layouts/
|
||||||
const prefixed_name = try std.mem.concat(self.allocator, u8, &[_][]const u8{ "layouts_", layout_name });
|
const prefixed_name = try std.mem.concat(self.allocator, u8, &[_][]const u8{ "layouts_", layout_name });
|
||||||
|
@ -38,6 +38,7 @@ pub const ViewType = union(enum) {
|
|||||||
|
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
action: Action,
|
action: Action,
|
||||||
|
view_name: []const u8,
|
||||||
uri_path: []const u8,
|
uri_path: []const u8,
|
||||||
view: ?ViewType = null,
|
view: ?ViewType = null,
|
||||||
static_view: ?StaticViewType = null,
|
static_view: ?StaticViewType = null,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user