A Two-step Dance with
Django and Jinja2
Templates
Sebastien Mirolo
@seabasscapoeira
smirolo@djaodjin.com
Motivation
Where we started
- 80 template files
- 4.3K lines of HTML template code
+ Customers relying
on site to process payments
The Plan
Deploy Django /
Jinja2 compatible
subset
Feature flag to
toggle between
template pipelines
An Easy Bit
{% csrf_token %}
-{% csrf_token %}
+<input type="hidden" name=“csrfmiddlewaretoken"
value="{{csrf_token}}">
The Good Old Times
$ cat base.html
{% block bodyscripts %}
<script type="text/javascript" charset="utf-8"
src=“/assets/base.js"></script>
{% block base_bodyscripts %}{% endblock %}
{% endblock %}
$ cat index.html
{% extends "base.html" %}
{% block bodyscripts %}
{% block base_bodyscripts %}
{{ block.super }}
<script type="text/javascript" charset="utf-8"
src="/assets/index.js"></script>
Undefined Context
Variables
{% if urls.profile %}
<a href="{{urls.profile}}">My Profile</a>
{% endif %}
When everything can
be solved with a filter
$ cat
templatetags/authenticated_tags.py
...
@register.filter()
def is_authenticated(request):
return hasattr(request, 'user') and
request.user.is_authenticated()
$ cat index.html
{% if request.user|is_authenticated %}
...
{% endif %}
is_authenticated
vs.
is_authenticated(
)
capfirst
vs.
capitalize
Moving Python code
into Templates
• crispy
• {% include with %} vs. {% macro
%}
Moving Template
code into Python
• URL patterns
• {% if forloop.counter0|divisible_by:3 %} vs. {% if
loop.index is divisibleby 3 %}
Jinja2 rendering
pipeline quirks
• Template loaders
• Cache dimensioning
• Optimizer in a multi-tier context
How you can help?
• Clone DjaoDjin repos and try them
out!
• Star us on GitHub!
https://github.com/djaodjin/djaodjin-pages/
https://github.com/djaodjin/djaodjin-saas/
Thank You
Sebastien Mirolo
@seabasscapoeira
smirolo@djaodjin.com

A Two-step Dance with Django and Jinja2 Templates

  • 1.
    A Two-step Dancewith Django and Jinja2 Templates Sebastien Mirolo @seabasscapoeira smirolo@djaodjin.com
  • 2.
  • 3.
    Where we started -80 template files - 4.3K lines of HTML template code + Customers relying on site to process payments
  • 4.
    The Plan Deploy Django/ Jinja2 compatible subset Feature flag to toggle between template pipelines
  • 5.
    An Easy Bit {%csrf_token %} -{% csrf_token %} +<input type="hidden" name=“csrfmiddlewaretoken" value="{{csrf_token}}">
  • 6.
    The Good OldTimes $ cat base.html {% block bodyscripts %} <script type="text/javascript" charset="utf-8" src=“/assets/base.js"></script> {% block base_bodyscripts %}{% endblock %} {% endblock %} $ cat index.html {% extends "base.html" %} {% block bodyscripts %} {% block base_bodyscripts %} {{ block.super }} <script type="text/javascript" charset="utf-8" src="/assets/index.js"></script>
  • 7.
    Undefined Context Variables {% ifurls.profile %} <a href="{{urls.profile}}">My Profile</a> {% endif %}
  • 8.
    When everything can besolved with a filter $ cat templatetags/authenticated_tags.py ... @register.filter() def is_authenticated(request): return hasattr(request, 'user') and request.user.is_authenticated() $ cat index.html {% if request.user|is_authenticated %} ... {% endif %} is_authenticated vs. is_authenticated( ) capfirst vs. capitalize
  • 9.
    Moving Python code intoTemplates • crispy • {% include with %} vs. {% macro %}
  • 10.
    Moving Template code intoPython • URL patterns • {% if forloop.counter0|divisible_by:3 %} vs. {% if loop.index is divisibleby 3 %}
  • 11.
    Jinja2 rendering pipeline quirks •Template loaders • Cache dimensioning • Optimizer in a multi-tier context
  • 12.
    How you canhelp? • Clone DjaoDjin repos and try them out! • Star us on GitHub! https://github.com/djaodjin/djaodjin-pages/ https://github.com/djaodjin/djaodjin-saas/
  • 13.

Editor's Notes

  • #2 http://www.wpclipart.com/cartoon/people/angry_with_axe_2.png.html
  • #4 photo credit: Happip (http://www.flickr.com/photos/46629025@N02/5453823369) cc 2.0