Difference between revisions of "Jinja"
Jump to navigation
Jump to search
(Created page with " ``` $ export MYENVVAR=foo ``` $ nano example.py ``` from jinja2 import Template import os template = Template("Hello {{ env['MYENVVAR'] or 'DefaultVal' }}") r = template.ren...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
$ nano example.py | $ nano example.py | ||
``` | ``` | ||
− | from jinja2 import Template | + | from jinja2 import Template, StrictUndefined |
import os | import os | ||
− | template = Template("Hello {{ env['MYENVVAR'] or 'DefaultVal' }}") | + | template = Template("Hello {{ env['MYENVVAR'] or 'DefaultVal' }}", undefined=StrictUndefined) |
r = template.render(env=os.environ, name='somethingelse') | r = template.render(env=os.environ, name='somethingelse') | ||
print(r) | print(r) | ||
Line 19: | Line 19: | ||
https://jinja.palletsprojects.com/en/2.11.x/intro/ | https://jinja.palletsprojects.com/en/2.11.x/intro/ | ||
+ | |||
+ | |||
+ | # Other Methods | ||
+ | |||
+ | ``` | ||
+ | targs = {} | ||
+ | targs['MYENVVAR'] = os.getenv('MYENVVAR') | ||
+ | |||
+ | |||
+ | with open('configMap.yml.jinja') as f_: | ||
+ | template = Template(f_.read(), undefined=StrictUndefined) | ||
+ | txt = template.render(targs) | ||
+ | with open('configMap.yml', 'w') as f_: | ||
+ | f_.write(txt) | ||
+ | ``` |
Latest revision as of 16:15, 10 October 2021
$ export MYENVVAR=foo
$ nano example.py
from jinja2 import Template, StrictUndefined import os template = Template("Hello {{ env['MYENVVAR'] or 'DefaultVal' }}", undefined=StrictUndefined) r = template.render(env=os.environ, name='somethingelse') print(r)
Run template
$ python3 example.py
https://jinja.palletsprojects.com/en/2.11.x/intro/
Other Methods
targs = {} targs['MYENVVAR'] = os.getenv('MYENVVAR') with open('configMap.yml.jinja') as f_: template = Template(f_.read(), undefined=StrictUndefined) txt = template.render(targs) with open('configMap.yml', 'w') as f_: f_.write(txt)