PYTHON TEMPLATES
DESIGN GUIDELINES
RIGHT WAY TO MAKE THINGS BAD
DO YOUR OWN JINJA2
PYTHON TEMPLATES GUIDELINES
• make ad-hoc loading machinery (template loaders)
• reinvent semi programming language (filters, includes, templatetags)
• hide functions and prohibit direct usage
• make it hard to use with other template engines
• don’t help to generate HTML
FROM DJANGO.TEMPLATE.LOADER IMPORT
RENDER_TO_STRING
TEMPLATE LOADING
• reinvent the wheel everytime
• dont use python one
Filters
Are functions
Macros
Are functions
Includes
Are functions?
INTEROPERABILITY
COMPATIBILITY
• We want to use this new fast and shiny new templates!
• Rails way — ok, `gem install new_shiny_project`, start to use
• Python way — no way, we need to rewrite everything! Pain and
suffer, cry everywhere.
• Why? Jinja2 awaits that loaders return text, not ready to use
function.
BACKSLANT
When someone broke rules
ALTERNATIVE GUIDELINES
BACKSLANT
• use python import to load templates
• functions for everything
• generate HTML from jade/slim/haml like lang
• play nice with jinja2
IN ACTION
BACKSLANT
html
body
a(href="")
'ABOUT'
<html>
<body>
<a herf="/genious/link">ABOUT</a>
...
</body>
</html>
IN ACTION
BACKSLANT
- def input(name, label):
div.pretty-input-sm2
label(for=(name))
= name
input(name=(name))
- def render(about):
html
body
a(href="")
‘ABOUT'
= about
form
== input(‘username’, ‘Your name’)
import backslant
backslant.install_loader()
from project.templates.main import render
@app.route(‘/‘)
def main(req):
return backslant.string(
render(
about=“HELLO”
)
)
JINJA2 INTEROPERABILITY
BACKSLANT
- from backslant.flask import extend_jinja2, include_jinja2
:call extend_jinja2('layouts/base.html')
- def content(ctx):
h2 "And something about other template engines"

Backslant or python templates engines design guidelines.