Skip to content

Instantly share code, notes, and snippets.

@chriskief
Created November 6, 2013 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskief/7344542 to your computer and use it in GitHub Desktop.
Save chriskief/7344542 to your computer and use it in GitHub Desktop.
from django import template
register = template.Library()
@register.tag(name='captureas')
def do_captureas(parser, token):
try:
tag_name, args = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError("'captureas' node requires a variable name.")
nodelist = parser.parse(('endcaptureas',))
parser.delete_first_token()
return CaptureasNode(nodelist, args)
class CaptureasNode(template.Node):
def __init__(self, nodelist, varname):
self.nodelist = nodelist
self.varname = varname
def render(self, context):
output = self.nodelist.render(context)
context[self.varname] = output
return ''
@tenhobi
Copy link

tenhobi commented Sep 10, 2017

Awesome! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment