Created
May 28, 2016 18:27
-
-
Save sparkprime/5b2ab0a1b72beceab2cf5ea524db228c to your computer and use it in GitHub Desktop.
Expanding Jinja within Jsonnet via Python bindings
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
local expand_template(template, vars) = | |
std.native("expand_template")(template, std.toString(vars)); | |
{ | |
response: expand_template(||| | |
Hello {{ title }} {{ family_name }}, please take a seat. | |
|||, { | |
title: "Dr", | |
family_name: "Frankenstein", | |
}), | |
} |
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
import jinja2 | |
import json | |
import _jsonnet | |
import sys | |
def expand_template(template, env_json): | |
env = json.loads(env_json) | |
r = jinja2.Environment().from_string(template).render(**env) | |
return r.encode('utf8') | |
native_callbacks = { | |
'expand_template': (('template', 'env_json'), expand_template), | |
} | |
json_str = _jsonnet.evaluate_file( | |
'test.jsonnet', | |
native_callbacks=native_callbacks, | |
) | |
sys.stdout.write(json_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment