Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mlouro/427116 to your computer and use it in GitHub Desktop.

Select an option

Save mlouro/427116 to your computer and use it in GitHub Desktop.

Revisions

  1. mlouro revised this gist Jun 7, 2010. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions Django {% include_partial %} template tag
    Original 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():
    try:
    context_params[k] = eval(v)
    except:
    context_params[k] = Variable(v).resolve(context)
    context_params[k] = Variable(v).resolve(context)

    t = get_template('%s' % self.partial)

  2. mlouro revised this gist Jun 7, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Django {% include_partial %} template tag
    Original 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('"') or partial.startswith("'"):
    if partial.startswith('"'):
    partial = partial[1:-1]

    for bit in bits[2:]:
  3. mlouro revised this gist Jun 6, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Django {% include_partial %} template tag
    Original 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 %}'
  4. mlouro created this gist Jun 6, 2010.
    52 changes: 52 additions & 0 deletions Django {% include_partial %} template tag
    Original 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)