Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include README.rst
include package.json
recursive-include armstrong/esi/templates *.html
prune build/*
1 change: 1 addition & 0 deletions armstrong/esi/templates/esi/esi_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<esi:include src="{{ esi_url }}" />
21 changes: 15 additions & 6 deletions armstrong/esi/templatetags/esi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import copy
from django import template
from django.template import defaulttags
from django.template.defaulttags import URLNode
from django.template.loader import render_to_string


register = template.Library()
esi_tmpl = '<esi:include src="%s" />'

class EsiTemplateTagError(Exception):
pass
Expand All @@ -25,14 +26,22 @@ def render(self, context):
except KeyError:
raise EsiTemplateTagError('The esi templatetag requires the esi context processor, but it isn\'t present.')

url = self.raw_url or super(EsiNode, self).render(context)
if self.raw_url:
url = self.raw_url
else:
url = super(EsiNode, self).render(context)
if self.asvar:
url = context[self.asvar]

render_context = copy.copy(context)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why fully copy the context instead of doing a context.push() before rendering the esi tag template and then popping the context afterwards?

render_context['esi_url'] = url
result = render_to_string('esi/esi_tag.html', render_context)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of making the esi template configurable, I'd personally be in favor of the string 'esi/esi_tag.html' being a configurable setting. Ever since switching to jinja i've been driven slightly crazy by hardcoded template locations. This is inside an actual template tag, so it's probably not going to be used by jinja users any time soon, but another getattr of settings won't break the bank


if self.asvar:
url = context[self.asvar]
context[self.asvar] = esi_tmpl % url
context[self.asvar] = result
return ''
else:
return esi_tmpl % url

return result

@register.tag
def esi(parser, token):
Expand Down