Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created February 4, 2012 00:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhulse/1734133 to your computer and use it in GitHub Desktop.
Save mhulse/1734133 to your computer and use it in GitHub Desktop.
[Django 1.3] Template tag that allows Django tags in variables (i.e. flatpage content).
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
# http://www.soyoucode.com/2011/set-variable-django-template
# http://djangosnippets.org/snippets/861/
# http://stackoverflow.com/questions/4183252/what-django-resolve-variable-do-template-variable
# https://docs.djangoproject.com/en/dev/ref/templates/api/
@register.tag
def allow_tags(parser, token):
"""
Example: {% allow_tags page.content %}
"""
try:
# Splitting by None == splitting by spaces:
tag_name, var_name = token.contents.split(None, 1)
except ValueError:
raise template.TemplateSyntaxError, '%r tag requires arguments' % token.contents.split()[0]
return RenderNode(var_name)
allow_tags.is_safe = True
class RenderNode(template.Node):
def __init__(self, content):
self.content = content
def render(self, context):
try:
content = template.Variable(self.content).resolve(context)
return template.Template(content).render(template.Context(context, autoescape=False))
except template.TemplateSyntaxError, e:
return mark_safe('Template error: There is an error one of this page\'s template tags: <code>%s</code>' % e.message)
{% extends "base.html" %}
{% load allow_tags %}
{% allow_tags page.content %}
@surdu
Copy link

surdu commented Aug 7, 2019

@mhulse FYI: change my website, so my article is now at https://surdu.me/2011/02/18/set-variable-django-template.html

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