mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-01-30 22:57:55 +00:00
lib/data: add function for rendering Mustache templates
This commit is contained in:
parent
b03032daff
commit
8a044c3eca
20
lib/data.nix
20
lib/data.nix
@ -44,4 +44,24 @@
|
||||
} ''
|
||||
tera --out "$out" ${extraArgs'} --template "${template}" "${contextFile}"
|
||||
'';
|
||||
|
||||
/* Render a Mustache template given a parameter set powered by `mustache-go`.
|
||||
Also typically used as an IFD.
|
||||
|
||||
Type: renderMustacheTemplate :: Attrs -> Path
|
||||
|
||||
Example:
|
||||
renderMustacheTemplate { path = ./template.mustache; context = { hello = 34; }; }
|
||||
=> /nix/store/HASH-mustache-render-template
|
||||
*/
|
||||
renderMustacheTemplate = { template, context, extraArgs ? { } }:
|
||||
let
|
||||
extraArgs' = lib.cli.toGNUCommandLineShell { } extraArgs;
|
||||
in pkgs.runCommand "mustache-render-template" {
|
||||
nativeBuildInputs = with pkgs; [ mustache-go ];
|
||||
context = builtins.toJSON context;
|
||||
passAsFile = [ "template" "context" ];
|
||||
} ''
|
||||
mustache "$contextPath" "${template}" ${extraArgs'} > $out
|
||||
'';
|
||||
}
|
||||
|
@ -30,4 +30,13 @@ lib.runTests {
|
||||
});
|
||||
expected = builtins.readFile ./fixtures/sample.tera;
|
||||
};
|
||||
|
||||
testRenderMustacheTemplate = {
|
||||
expr = builtins.readFile (self.data.renderTeraTemplate {
|
||||
template = ./templates/sample.mustache;
|
||||
context = lib.importJSON ./data/sample.json;
|
||||
});
|
||||
# There the same lol.
|
||||
expected = builtins.readFile ./fixtures/sample.tera;
|
||||
};
|
||||
}
|
||||
|
1
tests/lib/data/templates/sample.mustache
Normal file
1
tests/lib/data/templates/sample.mustache
Normal file
@ -0,0 +1 @@
|
||||
{{ hello }}
|
Loading…
Reference in New Issue
Block a user