"""
A decorator that abstracts away the boilerplate code around creating a template
node class which is the same each and every time you simply want a templatetag
with access to the template context and the calling token.
Usage:
@register_render_tag
def my_tag(context, token):
# parse the token and modify the context
return ""
Credits:
--------
Stephen McDonald <steve@jupo.org>
License:
--------
Creative Commons Attribution-Share Alike 3.0 License
http://creativecommons.org/licenses/by-sa/3.0/
When attributing this work, you must maintain the Credits
paragraph above.
"""
from django import template
register = template.Library()
def register_render_tag(renderer):
"""
Decorator that creates a template tag using the given renderer as the
render function for the template tag node - the render function takes two
arguments - the template context and the tag token
"""
def tag(parser, token):
class TagNode(template.Node):
def render(self, context):
return renderer(context, token)
return TagNode()
for copy_attr in ("__dict__", "__doc__", "__name__"):
setattr(tag, copy_attr, getattr(renderer, copy_attr))
return register.tag(tag)
Comments
Should be a built-in decorator
#