diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-04-12 21:07:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-12 21:07:53 +0200 |
commit | 8978c7eaf4b1a89712242c8626a217024aae2f2b (patch) | |
tree | 1248cd48ca5571d5d2098574ab932692767b5d2b /python | |
parent | ad489280ba7f4511016883c24a6d0b06b6659df8 (diff) | |
parent | 1fbaa2c59d0c0f43acad10db99d66b92fc520888 (diff) | |
download | vyos-1x-8978c7eaf4b1a89712242c8626a217024aae2f2b.tar.gz vyos-1x-8978c7eaf4b1a89712242c8626a217024aae2f2b.zip |
Merge pull request #332 from thomas-mangin/T2230
template: T2230: use render to generate templates
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/template.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py index e559120c0..6c73ce753 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -22,10 +22,17 @@ from vyos.defaults import directories # reuse the same Environment to improve performance -_templates_env = Environment(loader=FileSystemLoader(directories['templates'])) -_templates_mem = {} +_templates_env = { + False: Environment(loader=FileSystemLoader(directories['templates'])), + True: Environment(loader=FileSystemLoader(directories['templates']), trim_blocks=True), +} +_templates_mem = { + False: {}, + True: {}, +} -def render(destination, template, content): + +def render(destination, template, content, trim_blocks=False, formater=None): """ render a template from the template directory, it will raise on any errors destination: the file where the rendered template must be saved @@ -41,15 +48,18 @@ def render(destination, template, content): # Setup a renderer for the given template # This is cached and re-used for performance - if template not in _templates_mem: - _templates_mem[template] = _templates_env.get_template(template) - template = _templates_mem[template] + if template not in _templates_mem[trim_blocks]: + _templates_mem[trim_blocks][template] = _templates_env[trim_blocks].get_template(template) + template = _templates_mem[trim_blocks][template] # As we are opening the file with 'w', we are performing the rendering # before calling open() to not accidentally erase the file if the # templating fails content = template.render(content) + if formater: + content = formater(content) + # Write client config file with open(destination, 'w') as f: f.write(content) |