From e189abf30fe642bfeef68ba19a8941f187adf932 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Thu, 22 Jan 2026 23:36:42 +0530 Subject: [PATCH] fix: build template bug for same field names --- examples/demo/views/mixins/input_text.pug | 4 ++++ examples/demo/views/page-a.pug | 5 +++++ src/build_templates.zig | 13 ++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 examples/demo/views/mixins/input_text.pug diff --git a/examples/demo/views/mixins/input_text.pug b/examples/demo/views/mixins/input_text.pug new file mode 100644 index 0000000..a38ab3a --- /dev/null +++ b/examples/demo/views/mixins/input_text.pug @@ -0,0 +1,4 @@ +mixin input_text(name, label, placeholder) + fieldset.fieldset + legend.fieldset-legend= label + input(type="text" name=name class="input" placeholder=placeholder) diff --git a/examples/demo/views/page-a.pug b/examples/demo/views/page-a.pug index 6f479ab..c12ee23 100644 --- a/examples/demo/views/page-a.pug +++ b/examples/demo/views/page-a.pug @@ -19,3 +19,8 @@ block content "data": true } `) + form(method="post") + +input_text("firstName", "First Name", "first name") + br + +input_text("lastName", "Last Name", "last name") + submit sumit diff --git a/src/build_templates.zig b/src/build_templates.zig index f6efdcc..142f82d 100644 --- a/src/build_templates.zig +++ b/src/build_templates.zig @@ -1309,7 +1309,7 @@ const Compiler = struct { fn emitMixinCallWithDef(self: *Compiler, call: ast.MixinCall, mixin_def: ast.MixinDef) anyerror!void { // For each mixin parameter, we need to create a local binding - // This is complex in compiled mode - we inline the mixin body + // Wrap in a scope block to avoid variable name collisions when mixin is called multiple times // Save current mixin params const prev_params_len = self.mixin_params.items.len; @@ -1324,6 +1324,11 @@ const Compiler = struct { // Emit local variable declarations for mixin parameters try self.flush(); + // Open scope block for mixin variables + try self.writeIndent(); + try self.writer.writeAll("{\n"); + self.depth += 1; + for (mixin_def.params[0..regular_params], 0..) |param, i| { try self.mixin_params.append(self.allocator, param); @@ -1386,6 +1391,12 @@ const Compiler = struct { try self.emitNode(child); } } + + // Close scope block + try self.flush(); + self.depth -= 1; + try self.writeIndent(); + try self.writer.writeAll("}\n"); } /// Try to load a mixin from the mixins directory