diff options
author | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-11 19:32:25 +0100 |
---|---|---|
committer | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-12 20:05:13 +0100 |
commit | 1fbaa2c59d0c0f43acad10db99d66b92fc520888 (patch) | |
tree | 1248cd48ca5571d5d2098574ab932692767b5d2b /python/vyos/template.py | |
parent | ad489280ba7f4511016883c24a6d0b06b6659df8 (diff) | |
download | vyos-1x-1fbaa2c59d0c0f43acad10db99d66b92fc520888.tar.gz vyos-1x-1fbaa2c59d0c0f43acad10db99d66b92fc520888.zip |
template: T2230: use render to generate templates
convert all call to jinja to use template.render
Diffstat (limited to 'python/vyos/template.py')
-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) |