diff --git a/poleno/utils/template.py b/poleno/utils/template.py index 1469f576..ef4bcd20 100644 --- a/poleno/utils/template.py +++ b/poleno/utils/template.py @@ -120,7 +120,7 @@ def get_template_sources(self, template_name, template_dirs=None): class Library(template.Library): - def simple_pair_tag(self, func=None, takes_context=None, name=None): + def simple_pair_tag(self, func=None, takes_context=None, name=None, lazy_content=None): def compiler(parser, token, params, varargs, varkw, defaults, name, takes_context, node_class): @@ -147,17 +147,19 @@ def __init__(self, takes_context, nodelist, args, kwargs): self.args = args self.kwargs = kwargs - def get_resolved_arguments(self, context): + def render(self, context): resolved_args = [var.resolve(context) for var in self.args] - if self.takes_context: - resolved_args = [context] + resolved_args - resolved_args = [self.nodelist.render(context)] + resolved_args resolved_kwargs = dict((k, v.resolve(context)) for k, v in self.kwargs.items()) - return resolved_args, resolved_kwargs - def render(self, context): - resolved_args, resolved_kwargs = self.get_resolved_arguments(context) - return func(*resolved_args, **resolved_kwargs) + if lazy_content: + content = self.nodelist.render + else: + content = self.nodelist.render(context) + + if self.takes_context: + return func(content, context, *resolved_args, **resolved_kwargs) + else: + return func(content, *resolved_args, **resolved_kwargs) params, varargs, varkw, defaults = getargspec(func) function_name = (name or getattr(func, u'_decorated_function', func).__name__) diff --git a/poleno/utils/templatetags/poleno/amend.py b/poleno/utils/templatetags/poleno/amend.py index 041af572..4f6fa0e8 100644 --- a/poleno/utils/templatetags/poleno/amend.py +++ b/poleno/utils/templatetags/poleno/amend.py @@ -29,7 +29,7 @@ def _combine_parts(parent, parts): return parent -@register.simple_pair_tag(takes_context=True) +@register.simple_pair_tag(takes_context=True, lazy_content=True) def amend(content, context): u""" Edit a snippet of HTML by parsing it, manipulating its element tree and exporting it back to @@ -43,15 +43,14 @@ def amend(content, context): Adds a paragraph at the beginning of each form in `template.html`. """ - if u'_amend' not in context: - return content + with context.push(): + context[u'_amend'] = [] - fragment = lxml.html.fragment_fromstring(content, create_parent=u'root') - for action in context[u'_amend']: - fragment = action(fragment) + fragment = lxml.html.fragment_fromstring(content(context), create_parent=u'root') + for action in context[u'_amend']: + fragment = action(fragment) - del context[u'_amend'] - return lxml.html.tostring(fragment)[6:-7] # Strip root tag + return lxml.html.tostring(fragment)[6:-7] # Strip root tag @register.simple_pair_tag(takes_context=True) def prepend(content, context, path): @@ -75,9 +74,6 @@ def prepend(content, context, path):