Advanced Use of jinja2 for Templates
-
Upload
keith-resar -
Category
Internet
-
view
93 -
download
4
Transcript of Advanced Use of jinja2 for Templates
![Page 1: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/1.jpg)
@KeithResar
![Page 2: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/2.jpg)
@KeithResar
![Page 3: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/3.jpg)
![Page 4: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/4.jpg)
<b>From:</b> Pro-Speller <[email protected]>
<br><b>Date:</b> %{{exec("%s" % ((datetime.today() - timedelta(minutes = 47)).strftime("%A, %B %e, %Y at %l:%M %p")))}}%
<br><b>To:</b> Andrea Jacobs <[email protected]>
<br><b>Subject:</b> Your Scan Results - %{{url_f}}%
<br>
![Page 5: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/5.jpg)
%{{INNER_TPL}}%
<tr style="%{{style( ('miss_tr',) )}}%"> <td colspan="2" style="%{{style( ('miss_td',) )}}%">
<div style="%{{style( ('word_div',) )}}%"> <span style="%{{style( ('word_span',) )}}%">%{{INNER.WORD}}%</span> : <span>%{{INNER.SUGGESTIONS}}%</span> </div>
<div style="%{{style( ('suggestions_div',) )}}%"> <span>%{{INNER.CONTEXT}}%</span> </div>
</td> </td>
%{{ENDINNER_TPL}}%
![Page 6: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/6.jpg)
> perl -p -i -e \ 's/^PermitRootLogin .*/PermitRootLogin no' \ sshd_config
![Page 7: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/7.jpg)
![Page 8: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/8.jpg)
https://github.com/pallets/jinja#philosophy
![Page 9: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/9.jpg)
{% extends "layout.html" %}{% block body %} <ul> {% for user in users %} <li><a href="{{ user.url }}">{{ user.username }}</a></li> {% endfor %} </ul>{% endblock %}
![Page 10: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/10.jpg)
Sandboxed Execution ModeEvery aspect of the template execution is monitored and explicitly whitelisted or blacklisted, whatever is preferred. This makes it possible to execute untrusted templates.
Template Inheritance Makes it possible to use the same or a similar layout for all templates.
Easy to Debug With a debug system that integrates template compile and runtime errors into the standard Python traceback system.
Configurable SyntaxFor instance you can reconfigure Jinja2 to better fit output formats such as LaTeX or JavaScript.
(skipped the features more relevant to high-volume usage such as in Django)
![Page 11: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/11.jpg)
![Page 12: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/12.jpg)
Accepts any text file typetext, xml, html, etc
Convention is to name files with .j2 extensionhttp.conf.j2, sshd_config.j2
Convention to place in the templates/ directorytemplates/http.conf.j2, templates/sshd_config.j2
![Page 13: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/13.jpg)
{{ ... }}
{% ... %}
{# ... #}
![Page 14: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/14.jpg)
Accessing Ansible variables like
{{ ansible_hostname }} {{ ansible_date_time.epoch }}
We can do math like {{ 1+3 }}
Standard data types, like my list: {{ ('a','b','c') }}
We can call standard methods associated with our types:
{{ "lorem ipsum".upper() }} {{ ['a','b','c'].pop() }}
![Page 15: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/15.jpg)
Inline Conditionals: {{ '[%s]' % page.title if page.title }}
Built-in Filters:
{{ my_var | default('my_var undefined') }}
Maths: abs, int, float, round, sumStr: capitalize, length, center, escape, lower, regexLists: join, last, sort, shuffle, json_query
![Page 16: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/16.jpg)
Loops:<ul>{% for href, caption in my_list }} <li><a href="{{ href }}">{{ caption }}</a></li>{% endfor %}</ul>
Conditionals:{% if kenny.sick %} Kenny is sick.{% elif kenny.dead %} You killed Kenny! You bastard!!!{% else %} Kenny looks okay --- so far{% endif %}
![Page 17: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/17.jpg)
{# Note: Nothing in the comment will be included in the template output {% for user in users %} ... {% endfor %}#}
![Page 18: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/18.jpg)
{"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'my_var' is undefined"}
{"changed": false, "failed": true, "msg": "AnsibleError: template error while templating string: expected token 'end of print statement', got '...' String: <...>"}
![Page 19: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/19.jpg)
![Page 20: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/20.jpg)
Include as first line in the template file:
![Page 21: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/21.jpg)
{% include "file1.j2" %}
{% include ['file1.j2', 'file2.j2'] %}
![Page 22: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/22.jpg)
Alternation:{% for row in rows %} <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>{% endfor %}
Empty Lists:<ul>{% for user in users %} <li>{{ user.username|e }}</li>{% else %} <li><em>no users found</em></li>{% endfor %}</ul>
![Page 23: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/23.jpg)
Sort by Attribute:
{% for item in iterable|sort(attribute='date') %} ...{% endfor %}
Group by Attribute:
{% for group in persons|groupby('gender') %} <li>{{ group.grouper }}<ul> {% for person in group.list %} <li>{{ person.first_name }}</li> {% endfor %}</ul></li>{% endfor %}
![Page 24: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/24.jpg)
![Page 25: Advanced Use of jinja2 for Templates](https://reader030.fdocuments.in/reader030/viewer/2022013104/58f9b306760da3da068bd1b0/html5/thumbnails/25.jpg)
@KeithResar
@KeithResar