Skip to content

Accept template tags in Django Flatpages

by admin on June 20th, 2010

You need to use template tags in Django FlatPages application ?

Here is a solution:

First create an file in your templatetags directory with the following content:

from django import template

register = template.Library()

def do_evaluate(parser, token):
    tag usage {% evaluate object.textfield %}
        tag_name, variable = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
    return EvaluateNode(variable)

class EvaluateNode(template.Node):
    def __init__(self, variable):
        self.variable = template.Variable(variable)

    def render(self, context):
            content = self.variable.resolve(context)
            t = template.Template(content)
            return t.render(context)
        except template.VariableDoesNotExist, template.TemplateSyntaxError:
            return 'Error rendering', self.variable

Next, modifiy your flatpages template file to use this template tag:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
<title>{{ flatpage.title }}</title>
{% load evaluate_tag %}
{% evaluate flatpage.content %}

Now, you can use template tags in your flatpages !

From → Tips

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS