fix: make conditional fields optional using @hasField

Templates can now use 'if error' or similar conditionals without
requiring the caller to always provide those fields in the data struct.
This commit is contained in:
2026-01-23 12:10:48 +05:30
parent 4f1dcf3640
commit 53f147f5c4
3 changed files with 15 additions and 4 deletions

View File

@@ -143,7 +143,7 @@ pub fn home(a: Allocator, d: anytype) Allocator.Error![]u8 {
try o.appendSlice(a, "</title><link rel=\"stylesheet\" href=\"/style.css\" /></head><body><header><h1>");
try esc(&o, a, strVal(@field(d, "title")));
try o.appendSlice(a, "</h1>");
if (truthy(@field(d, "authenticated"))) {
if (@hasField(@TypeOf(d), "authenticated") and truthy(@field(d, "authenticated"))) {
try o.appendSlice(a, "<span class=\"user\">Welcome back!</span>");
}
try o.appendSlice(a, "</header><main><p>This page is rendered using a compiled template.</p><p>Compiled templates are 3x faster than Pug.js!</p></main><footer><p>&copy; 2024 Pugz Demo</p></footer></body></html>");
@@ -209,7 +209,7 @@ pub fn page_a(a: Allocator, d: anytype) Allocator.Error![]u8 {
try o.appendSlice(a, " /></fieldset>");
}
try o.appendSlice(a, "<submit>sumit</submit>");
if (truthy(@field(d, "error"))) {
if (@hasField(@TypeOf(d), "error") and truthy(@field(d, "error"))) {
{
const message = @field(d, "error");
const mixin_attrs_1: struct {