lib/data: add function for rendering Mustache templates

This commit is contained in:
Gabriel Arazas 2024-07-27 20:26:26 +08:00
parent b03032daff
commit 8a044c3eca
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
3 changed files with 30 additions and 0 deletions

View File

@ -44,4 +44,24 @@
} '' } ''
tera --out "$out" ${extraArgs'} --template "${template}" "${contextFile}" 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
'';
} }

View File

@ -30,4 +30,13 @@ lib.runTests {
}); });
expected = builtins.readFile ./fixtures/sample.tera; 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;
};
} }

View File

@ -0,0 +1 @@
{{ hello }}