views:

25

answers:

1

I've got a view func like this:

def user_agreement(request):
    return response(request, template='misc/flatpage.html',
            vars={'fp':FlatPage.objects.get(key='user-agreement')})

And then the template looks like this:

<h2>{% block title %}{{ fp.title }}{% endblock %}</h2>

{{ fp.content|markdown }}

This works pretty well, but I also want to include some Django {{filters}} in the content. Is there an "evaluate" filter so I can do:

{{ fp.content|evaluate|markdown }}

And it will substitute all my variables for me? Or what's the easiest/best approach to this?

+2  A: 

I'm not sure if I understand your question correctly, but the following might work.

Treat flatpage.content as a template, and render it in the view with any context you wish.

# view
from django.template import Template, Context

def user_agreement(request):
    flatpage = FlatPage.objects.get(key='user-agreement')
    t = Template(flatpage.content)
    fp_content = t.render(Context({}))
    return response(request, template='misc/flatpage.html',
        vars={'title': flatpage.title, 'content': fp_content}) 

Then apply the markdown filter in the misc/flatpage.html template.

<h2>{% block title %}{{ title }}{% endblock %}</h2>

{{ content|markdown }}
Alasdair
Yes, that's what I meant. I want to evaluate/parse it as a template. This is certainly one solution, but now I have to think about refactoring because I don't want to write that 10 times :)
Mark
If you only need to use filters in your flatpage content, then it should be straight forward to implement `evaluate` as a string filter. If it needs context then it's a bit trickier. Hope you come up with a nice DRY solution to avoid writing it 10 times :)
Alasdair