let’s talk about templates - about pocoo — pocoomitsuhiko/templates.pdflet’s talk about...
TRANSCRIPT
![Page 1: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/1.jpg)
Let’s Talk About Templates
Armin Ronacher@mitsuhiko
![Page 2: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/2.jpg)
Templateswhy are we discussing templates in 2014?
![Page 3: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/3.jpg)
In 2011 we all thought single page applications are the future
![Page 4: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/4.jpg)
“Cloud” Performance > Phone Performance
![Page 5: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/5.jpg)
It's really hard to make a nice, JS heavy UI
![Page 6: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/6.jpg)
Server Side Rendering is Fancy Again(at least for us)
![Page 7: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/7.jpg)
“Talk about Performance”every invitation about a template engine talk ever
![Page 8: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/8.jpg)
History LessonsHistory of Python Template Engines
![Page 9: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/9.jpg)
Django and Jinja and the Greater Picture
❖ 2000: mod_python
❖ 2003: Webware for Python (-> wsgikit -> paste -> webob)
❖ 2003: WSGI spec
❖ 2005: Django
❖ 2006: Jinja
❖ 2008: Jinja2
❖ 2014: haven't touched templates in years!
(story not continued)
![Page 10: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/10.jpg)
Personal GrowthWhy I have a hard time talking about Jinja today
![Page 11: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/11.jpg)
Armin and Jinja
❖ Armin learning programming: 2003
❖ Armin learning Python: 2004
❖ Django’s first public release: July 2005
❖ Jinja’s first public release: January 2006
❖ Jinja2: June 2008
![Page 12: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/12.jpg)
Jinja2 has bugs, bug fixing some of them would probably break people’s templates
![Page 13: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/13.jpg)
Jinja’s Problems
❖ Hand written lexer with problematic operator priorities
❖ Slightly incorrect identifier tracking
❖ Non ideal semantics for included templates
❖ Slow parsing and compilation step
![Page 14: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/14.jpg)
not broken enough for a rewrite
(there����������� ������������������ won’t����������� ������������������ be����������� ������������������ a����������� ������������������ Jinja����������� ������������������ 3)
![Page 15: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/15.jpg)
How do they work?What makes a template engine work
![Page 16: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/16.jpg)
Template Engine Design
❖ Django and Jinja2 differ greatly on the internal design
❖ Django is an AST interpreter with made up semantics
❖ Jinja is a transpiler with restricted semantics to aid compilation
![Page 17: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/17.jpg)
General Preprocessing Pipeline
❖ Load template source
❖ Feed source to lexer for tokenization
❖ Parser converts tokens into an AST (Abstract Syntax Tree)
❖ -> Compile to Bytecode
❖ -> Keep AST for later evaluation
![Page 18: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/18.jpg)
Rendering Pipeline
❖ Create a context object with all data for the template
❖ Take AST/bytecode
❖ pass context and AST/bytecode to render system
❖ acquire result
![Page 19: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/19.jpg)
The DifferencesHow do Jinja2 and Django differ?
![Page 20: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/20.jpg)
❖ Evaluates Bytecode ❖ Evaluates AST
What they do when they render
![Page 21: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/21.jpg)
❖ Overarching Grammar
❖ As the lexer encounters a block opener tag it will switch it’s parsing state
❖ Allows arbitrary nesting of lexial constructs
❖ Two stage grammar
❖ Lexer splits template into tokens in the form “block”, “variable”, “comment” and “template data”
❖ Second stage lexer splits tokens into smaller ones
❖ No nesting
![Page 22: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/22.jpg)
❖ BLOCK_START
❖ NAME "if"
❖ IDENT "expr"
❖ BLOCK_END
❖ DATA "…"
❖ BLOCK_START
❖ NAME "endif"
❖ BLOCK_END
❖ BLOCK "if expr"
❖ DATA "…"
❖ BLOCK "endif"
{% if expr %}...{% endif %}
![Page 23: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/23.jpg)
{{ "{{foo}}" }} {% templatetag commentopen %}foo{% templatetag commentclose %}
Render: {{foo}}
![Page 24: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/24.jpg)
❖ Nodes in Jinja act as AST
❖ The AST gets processed and compiled into Python code
❖ Nodes are thrown away post compilation
❖ Nodes in Django are kept in memory
❖ Upon evaluation their callbacks are invoked
❖ Callbacks render the template recursively into strings
![Page 25: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/25.jpg)
❖ Overarching Grammar
❖ As the lexer encounters a block opening tag it will switch it’s parsing state
❖ Allows arbitrary nesting of lexial constructs
❖ Two stage grammar
❖ Lexer splits template into tokens in the form “block”, “variable”, “comment” and “template data”
❖ Second stage lexer splits tokens into smaller ones
❖ No nesting
![Page 26: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/26.jpg)
❖ heavily discouraged
❖ syntax consistent with Jinja core
❖ need to generate Jinja nodes
❖ tricky to debug due to compiled nature
❖ encouraged and ubiquitous
❖ can and do have custom syntax
❖ easy to implement due to the render method and context object
❖ debugging possible within Django due to the debug middleware
![Page 27: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/27.jpg)
❖ compiles into a generator yielding string chunks.
❖ proper recursive calls will buffer
❖ syntax supported recursion will forward iterators
❖ each render function yields a string
❖ any form of recursive calls will need to assemble a new string
![Page 28: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/28.jpg)
❖ keeps source information
❖ integrates into Python traceback, supports full recursion including calls to Python and back to Jinja
❖ Customizable behavior for missing variables
❖ keeps simplified source location on nodes
❖ uses it's own error rendering and for performance reasons cannot provide more accurate information
❖ Missing var = empty string
![Page 29: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/29.jpg)
❖ Source of data
❖ Only holds top-level variables
❖ Two-layer dictionary, optionally linked to a parent scope but not resolved through
❖ Store of data
❖ Holds all variables
❖ Stack of dictionaries
![Page 30: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/30.jpg)
❖ uses markupsafe
❖ escaping is “standardized”
❖ lives in Python
❖ the only integration in the template engine is:
❖ awareness in the optimizer
❖ enables calls to escape() for all printed expressions
❖ Django specific
❖ lives largely only in the template engine with limited support in Python
❖ Django one-directionally supports the markupsafe standard
![Page 31: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/31.jpg)
markupsafe
class Foo(object):
def __html__(self): return Markup(u'This object in HTML context')
def __unicode__(self): return u'This object in text context'
>>> Markup('<em>%s</em>') % '<script>alert(document.cookie)</script>' Markup(u'<em><script>alert(document.cookie)</script></em>')
![Page 32: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/32.jpg)
Django's TemplatesHow it renders and does things
![Page 33: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/33.jpg)
Parsing after “Tokenizing”
❖ look at first name
❖ load “parsing callback for name”
❖ parsing callback might or might not use “token splitting function”
❖ parsing callback creates a node
![Page 34: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/34.jpg)
Templates are really old
❖ whoever wrote it, learned what an AST interpreter is
❖ someone else changed it afterwards and forgot that the idea is, that it's not mutating the state of nodes while rendering
❖ only after Jinja2's release could Django cache templates because rendering stopped mutating state :)
![Page 35: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/35.jpg)
How it Represents
NodeList([ TextNode("Hello "), VariableNode(FilterExpression( var=Variable("variable"), filters=[("escape", ()]) ) ])
Hello {{ variable|escape }}
![Page 36: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/36.jpg)
How it Renders
class NodeList(list):
def render(self, context): bits = [] for node in self: if isinstance(node, Node): bit = node.render(context) else: bit = node bits.append(force_text(bit)) return mark_safe(''.join(bits))
Hello {{ variable|escape }}
![Page 37: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/37.jpg)
Complex Nodes
class IfNode(Node):
def __init__(self, conditions_nodelists): self.conditions_nodelists = conditions_nodelists
def render(self, context): for condition, nodelist in self.conditions_nodelists: if condition is not None: try: match = condition.eval(context) except VariableDoesNotExist: match = None else: match = True if match: return nodelist.render(context) return ''
{% if item %}...{% endif %}
![Page 38: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/38.jpg)
Jinja is ComplexJinja does things because it can
![Page 39: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/39.jpg)
Basic Transpiling
def root(context): l_variable = context.resolve('variable') t_1 = environment.filters['escape'] yield u'Hello ' yield escape(t_1(l_variable))
Hello {{ variable|escape }}
![Page 40: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/40.jpg)
Knowledge Allows Optimizations
def root(context): yield u'Hello <World>!'
Hello {{ "<World>!"|escape }}
![Page 41: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/41.jpg)
Different Transformations
def root(context): l_seq = context.resolve('seq') l_item = missing for l_item in l_seq: yield u'<li>' yield escape(l_item) l_item = missing
{% for item in seq %}<li>{{ item }}{% endfor %}
![Page 42: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/42.jpg)
Different Transformations
def root(context): l_seq = context.resolve('seq') l_item = missing l_loop = missing for l_item, l_loop in LoopContext(l_seq): yield u'<li>%s: %s' % ( escape(environment.getattr(l_loop, 'index')), escape(l_item), ) l_item = missing
{% for item in seq %}<li>{{ loop.index }}: {{ item }}{% endfor %}
![Page 43: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/43.jpg)
Block Handling
def root(context): yield u'<title>' for event in context.blocks['title'][0](context): yield event yield u'</title>'
def block_title(context): yield u'Default Title'
blocks = {'title': block_title}
<title>{% block title %}Default Title{% endblock %}</title>
![Page 44: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/44.jpg)
Super Calls
def root(context): parent_template = None parent_template = environment.get_template('layout', None) for name, parent_block in parent_template.blocks.iteritems(): context.blocks.setdefault(name, []).append(parent_block) for event in parent_template.root_render_func(context): yield event
def block_title(context): l_super = context.super('title', block_title) yield escape(context.call(l_super))
blocks = {'title': block_title}
{% extends "layout" %}{% block title %}{{ super() }}{% endblock %}
![Page 45: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/45.jpg)
Errors
Traceback (most recent call last): File "example.py", line 7, in <module> print tmpl.render(seq=[3, 2, 4, 5, 3, 2, 0, 2, 1]) File "jinja2/environment.py", line 969, in render return self.environment.handle_exception(exc_info, True) File "jinja2/environment.py", line 742, in handle_exception reraise(exc_type, exc_value, tb) File "templates/broken.html", line 4, in top-level template code <li>{{ may_break(item) }}</li> File "templates/subbroken.html", line 2, in template [{{ item / 0 }}] ZeroDivisionError: division by zero
{% macro may_break(item) -%} [{{ item / 0 }}] {%- endmacro %}
![Page 46: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/46.jpg)
Make one like the otherAbout the many attempts of making Django like Jinja
![Page 47: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/47.jpg)
Why make one like the other?
❖ People like Jinja because of
❖ expressions
❖ performance
❖ People like Django because of
❖ extensibility
![Page 48: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/48.jpg)
The Performance Problem
❖ Jinja is largely fast because it choses to “not do things”:
❖ it does not have a context
❖ it does not have loadable extensions
❖ if it can do nothing over doing something, it choses nothing
❖ it tracks identifier usage to optimize code paths
![Page 49: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/49.jpg)
Why can't Django do that?
❖ Jinja needed to sacrifice certain functionality
❖ Doing the same in Django would break everybody's code
![Page 50: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/50.jpg)
Why not make a Jinja Inspired Django?
❖ Making the Django templates like Jinja2 would be a Python 3 moment
❖ There would have to be a migration path (allow both to be used)
❖ Cost / Benefit relationship is not quite clear
![Page 51: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/51.jpg)
Pluggable Template Engines?
❖ Most likely success
❖ Could start switching defaults over at one point
❖ Pluggable apps might not like it :(
![Page 52: Let’s Talk About Templates - About Pocoo — Pocoomitsuhiko/Templates.pdfLet’s Talk About Templates Armin Ronacher @mitsuhiko Templates why are we discussing templates in 2014?](https://reader030.vdocuments.mx/reader030/viewer/2022021520/5b382b2f7f8b9a5a178d0a9d/html5/thumbnails/52.jpg)
Questions and AnswersSlides will be at lucumr.pocoo.org/talks
Contact via [email protected] Twitter: @mitsuhiko
If you have interesting problems, you can hire me :)