From 844b35589e31deec76cd997567e43a4262d28c80 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 10 May 2018 01:14:53 +0200 Subject: [PATCH] Add ynh_render_template helper to render templates with jinja2 --- data/helpers.d/utils | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data/helpers.d/utils b/data/helpers.d/utils index cc057eb0b..05f9a4703 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -234,3 +234,20 @@ ynh_local_curl () { # Curl the URL curl --silent --show-error -kL -H "Host: $domain" --resolve $domain:443:127.0.0.1 $POST_data "$full_page_url" } + +# Render templates with Jinja2 +# +# Attention : Variables should be exported before calling this helper to be +# accessible inside templates. +# +# usage: ynh_render_template some_template output_path +# | arg: some_template - Template file to be rendered +# | arg: output_path - The path where the output will be redirected to +ynh_render_template() { + local template_path=$1 + local output_path=$2 + # Taken from https://stackoverflow.com/a/35009576 + python2.7 -c 'import os, sys, jinja2; sys.stdout.write( + jinja2.Template(sys.stdin.read() + ).render(os.environ));' < $template_path > $output_path +}