Jinja2: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(Created page with " <pre> [user@server common]$ python py_check some randome string The test worked [user@server common]$ cat py_check from jinja2 import Environment, FileSystemLoader env = Envi...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:


== COmmnad line testing ==
template files /home/user/project/files/common/test.j2
<pre>
<pre>
[user@server common]$ python py_check
{{ something }}
some randome string
The test worked
The test worked
[user@server common]$ cat py_check
</pre>
 
the python script:
<pre>
from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('/home/user/project/files/common'))
env = Environment(loader=FileSystemLoader('/home/user/project/files/common'))
Line 10: Line 16:
something = 'some randome string'
something = 'some randome string'
print tmpl.render(something=something)
print tmpl.render(something=something)
</pre>


[user@server common]$ cat test.j2
test run:
{{ something }}
<pre>
[user@server common]$ python py_check
some randome string
The test worked
The test worked
[user@server common]$
[user@server common]$
</pre>
== Conditionals ==
<pre>{% if '-esb-' in ansible_hostname %}
# hostname is {{ ansible_hostname }}
{% else %}
# {{ ansible_hostname }} did not match
{% endif %}
</pre>
</pre>

Latest revision as of 15:51, 29 July 2014

COmmnad line testing

template files /home/user/project/files/common/test.j2

{{ something }}
The test worked

the python script:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('/home/user/project/files/common'))
tmpl = env.get_template('test.j2')
something = 'some randome string'
print tmpl.render(something=something)

test run:

[user@server common]$ python py_check
some randome string
The test worked
[user@server common]$

Conditionals

{% if '-esb-' in ansible_hostname %}
# hostname is {{ ansible_hostname }}
{% else %}
# {{ ansible_hostname }} did not match
{% endif %}