Allow template tags in a Flatpage's content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

class RenderNode(template.Node):

    def __init__(self, content):
        self.content = content
    
    def render(self, context):
        try:
            self.content = template.resolve_variable(self.content, context)
            return template.Template(self.content).render(template.Context(context, autoescape=False))
        except template.TemplateSyntaxError, e:
            return mark_safe("<strong>Template error: There is an error one of this page's template tags: <code>%s</code></small>" % e.message)


@register.tag(name='render')
def render_django(parser, token):
    " Example: {% render flatpage.content %}"
    content = token.split_contents()[-1]
    return RenderNode(content)
render_django.is_safe = True

Comments

jezdez (on October 1, 2008):

Please have a look at the django-dbtemplates project that enables you to save templates in the database in an official way and uses an own template loader.

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.