Last active
August 18, 2025 18:42
-
-
Save vadimkantorov/1858ef274f9f131db5762202fafb5fb2 to your computer and use it in GitHub Desktop.
One-liner basic CLI for jinja2, context in https://github.com/pallets/jinja/issues/2113
This file contains hidden or 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
| echo '{"site": {"hello": "world"}}' > foo.json | |
| echo 'Hello {{ site.hello }}' > foo.txt.j2 | |
| bash j2.sh -t foo.txt.j2 -i foo.json -o foo.txt | |
| cat foo.txt | |
| source function.sh | |
| j2 -t foo.txt.j2 -i foo.json -o foo.txt | |
| cat foo.txt | |
| unset -f j2 | |
| # can copy-paste the alias definition into ~/.bashrc | |
| source alias.sh | |
| j2 -t foo.txt.j2 -i foo.json -o foo.txt | |
| cat foo.txt | |
| unalias j2 |
This file contains hidden or 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
| alias j2='python -c '\''import jinja2, argparse, json, sys, os; parser = argparse.ArgumentParser(); parser.add_argument("--input-path", "-i"); parser.add_argument("--output-path", "-o"); parser.add_argument("--template-path", "-t"); args = parser.parse_args(); environment = jinja2.Environment(); print(args.template_path, file = sys.stderr); template = environment.from_string(open(args.template_path).read()); print(args.input_path, file = sys.stderr); ctx = json.load(open(args.input_path)); rendered = template.render(**ctx); print(args.output_path, file = sys.stderr); os.makedirs(os.path.dirname(args.output_path) or ".", exist_ok=True); (open(args.output_path, "w") if args.output_path != "-" else sys.stdout).write(rendered);'\'' ' |
This file contains hidden or 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
| j2() { python -c 'import jinja2, argparse, json, sys, os; parser = argparse.ArgumentParser(); parser.add_argument("--input-path", "-i"); parser.add_argument("--output-path", "-o"); parser.add_argument("--template-path", "-t"); args = parser.parse_args(); environment = jinja2.Environment(); print(args.template_path, file = sys.stderr); template = environment.from_string(open(args.template_path).read()); print(args.input_path, file = sys.stderr); ctx = json.load(open(args.input_path)); rendered = template.render(**ctx); print(args.output_path, file = sys.stderr); os.makedirs(os.path.dirname(args.output_path) or ".", exist_ok=True); (open(args.output_path, "w") if args.output_path != "-" else sys.stdout).write(rendered);' $@; } |
This file contains hidden or 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
| python -c 'import jinja2, argparse, json, sys, os; parser = argparse.ArgumentParser(); parser.add_argument("--input-path", "-i"); parser.add_argument("--output-path", "-o"); parser.add_argument("--template-path", "-t"); args = parser.parse_args(); environment = jinja2.Environment(); print(args.template_path, file = sys.stderr); template = environment.from_string(open(args.template_path).read()); print(args.input_path, file = sys.stderr); ctx = json.load(open(args.input_path)); rendered = template.render(**ctx); print(args.output_path, file = sys.stderr); os.makedirs(os.path.dirname(args.output_path) or ".", exist_ok=True); (open(args.output_path, "w") if args.output_path != "-" else sys.stdout).write(rendered);' $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment