Skip to content

Instantly share code, notes, and snippets.

@ojii
Created October 16, 2012 11:37
Show Gist options
  • Save ojii/3898779 to your computer and use it in GitHub Desktop.
Save ojii/3898779 to your computer and use it in GitHub Desktop.
Idea for a nicer way to write template tags in Django
@doctag('''Sums two numbers
Usage:
{% sum <a> <b> [as <c>] %}
''')
def sum(context, a, b, c=None):
result = int(a) + int(b)
if c:
context[c] = result
else:
return result
@doctag('''Adds a new thing to the context
Usage:
{% with (<a>=<b>)... %}...{% endwith %}
''')
def with_tag(context, a, b, nodelist):
context.push()
for key, value in zip(a, b):
context[key] = value
output = nodelist.render(context)
context.pop()
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment