-
Notifications
You must be signed in to change notification settings - Fork 8
Render the esi tags with a template to be able to customize it depending... #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/* |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <esi:include src="{{ esi_url }}" /> |
| 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 | ||
|
|
@@ -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) | ||
| render_context['esi_url'] = url | ||
| result = render_to_string('esi/esi_tag.html', render_context) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
|
||
There was a problem hiding this comment.
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?