Created
June 6, 2010 00:02
-
-
Save mlouro/427116 to your computer and use it in GitHub Desktop.
Revisions
-
mlouro revised this gist
Jun 7, 2010 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,10 +14,7 @@ class PartialNode(Node): context_params = {} for k, v in self.params.items(): context_params[k] = Variable(v).resolve(context) t = get_template('%s' % self.partial) -
mlouro revised this gist
Jun 7, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ def include_partial(parser, token): params = {} try: tag_name, partial = bits[:2] if partial.startswith('"'): partial = partial[1:-1] for bit in bits[2:]: -
mlouro revised this gist
Jun 6, 2010 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,7 @@ class PartialNode(Node): def include_partial(parser, token): """ Include a template snippet (partial) with it's own context Heavily based on http://freeasinbeard.org/post/107743420/render-partial-in-django """ __doc__ = '{% include_partial "path/to/template.html" field=form.name label="string" arg3=foo %}' -
mlouro created this gist
Jun 6, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ import re from django.template import Context, Library, Node, Variable, TemplateSyntaxError from django.template.loader import get_template, render_to_string register = Library() class PartialNode(Node): def __init__(self, partial, params): self.partial = partial self.params = params def render(self, context): context_params = {} for k, v in self.params.items(): try: context_params[k] = eval(v) except: context_params[k] = Variable(v).resolve(context) t = get_template('%s' % self.partial) return t.render(Context(context_params)) @register.tag def include_partial(parser, token): """ Include a template snippet (partial) with it's own context """ __doc__ = '{% include_partial "path/to/template.html" field=form.name label="string" arg3=foo %}' bits = token.split_contents() if len(bits) == 1: raise TemplateSyntaxError('Accepted format: %s' % __doc__) params = {} try: tag_name, partial = bits[:2] if partial.startswith('"') or partial.startswith("'"): partial = partial[1:-1] for bit in bits[2:]: s, key, value = re.split('^(\w+)=', bit) params[key] = value except ValueError: raise TemplateSyntaxError('Accepted format: %s' % __doc__) return PartialNode(partial, params)