Skip to content

Instantly share code, notes, and snippets.

@sparkprime
Created May 28, 2016 18:27
Show Gist options
  • Save sparkprime/5b2ab0a1b72beceab2cf5ea524db228c to your computer and use it in GitHub Desktop.
Save sparkprime/5b2ab0a1b72beceab2cf5ea524db228c to your computer and use it in GitHub Desktop.
Expanding Jinja within Jsonnet via Python bindings
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",
}),
}
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