Skip to content

Instantly share code, notes, and snippets.

@dtcooper
Created November 7, 2011 21:47
Show Gist options
  • Save dtcooper/1346300 to your computer and use it in GitHub Desktop.
Save dtcooper/1346300 to your computer and use it in GitHub Desktop.
Conditional decorator to escape strings
from functools import wraps
from django.utils.safestring import mark_safe
def raw_html(func_or_str):
'''
Call me a string or decorator a function with me to output HTML in your template.
'''
if callable(func_or_str):
@wraps(func_or_str)
def decorated(*args, **kwargs):
return mark_safe(func_or_str(*args, **kwargs))
return decorated
else:
return mark_safe(func_or_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment