-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MustacheLoad to support loading from file
According to the facil.io docs (http://facil.io/0.7.x/fiobj_mustache#fiobj_mustache_new): > By setting the filename argument even when the data argument exists, > it will allow path resolution for partial templates. Otherwise, there > is no way to know where to find the partial templates. This will allow partial templates to work. However, this also introduces a breaking change to the existing MustacheNew function. This change makes it mirror the C version where it accepts a MustacheLoadArgs struct instead of just the data. That's also a handy method to have, so I renamed that to MustacheData...maybe there's a better name?
- Loading branch information
1 parent
ecb0602
commit 8f5aa17
Showing
6 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const std = @import("std"); | ||
const zap = @import("zap"); | ||
|
||
const User = struct { | ||
name: []const u8, | ||
id: isize, | ||
}; | ||
|
||
const data = .{ | ||
.users = [_]User{ | ||
.{ | ||
.name = "Rene", | ||
.id = 1, | ||
}, | ||
.{ | ||
.name = "Caro", | ||
.id = 6, | ||
}, | ||
}, | ||
.nested = .{ | ||
.item = "nesting works", | ||
}, | ||
}; | ||
|
||
test "MustacheData" { | ||
const template = "{{=<< >>=}}* Users:\n<<#users>><<id>>. <<& name>> (<<name>>)\n<</users>>\nNested: <<& nested.item >>."; | ||
const p = try zap.MustacheData(template); | ||
defer zap.MustacheFree(p); | ||
|
||
const ret = zap.MustacheBuild(p, data); | ||
defer ret.deinit(); | ||
|
||
try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.", ret.str().?); | ||
} | ||
|
||
test "MustacheLoad" { | ||
const p = try zap.MustacheLoad("./src/tests/testtemplate.html"); | ||
defer zap.MustacheFree(p); | ||
|
||
const ret = zap.MustacheBuild(p, data); | ||
defer ret.deinit(); | ||
|
||
try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.\n", ret.str().?); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nested: {{& nested.item }}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{=<< >>=}}* Users: | ||
<<#users>><<id>>. <<& name>> (<<name>>) | ||
<</users>> | ||
<<>testpartial.html>> |