First time here? Check out the FAQ!
1

How to perform Template() on askbot setting?

I'm pretty new to Python, and Django is even more unfamiliar, but I was trying to find a simple hack that would process a text-based askbot setting using the Django Template() function, specifically here:

http://askbot.org/en/question/519/faq-to-settings

Where what I would like to do something remotely like:

if getattr(askbot_settings, 'FORUM_FAQ',''):
    faq = Template(FORUM_FAQ)
    return render_into_skin(faq, data, request)

which of course doesn't work for various reasons. My question is, what exactly are the intermediate steps to get something like this to work, so that FORUM_FAQ can contain template tags like {{settings.MIN_REP_TO_VOTE_UP}}, be processed to replace these tags, then fed into an HTTP request like faq.htm?

It seems like it should be pretty easy, but again, I don't know either python or django (or askbot) well enough to figure it out.

NoahY's avatar
303
NoahY
asked 2011-07-23 08:46:58 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

4 Answers

1

Okay, I figured it out... I wasn't importing Template :)

Once I import Template, I add this to loaders.py:

def render_text(text, data, request):
        context = RequestContext(request, data)
        template = Template(text)
        return template.render(context)

And now I can send the text there first, then to render_into_skin.

The only problem I have with this is {% trans %} tags don't work. Otherwise, you can at least include settings variables now.

NoahY's avatar
303
NoahY
answered 2011-07-24 14:52:06 -0500
edit flag offensive 0 remove flag delete link

Comments

Hey, sorry for not answering. Askbot uses Jinja2 templates, and they use different template tags for translations. In general, Jinja2 templating engine is better and faster. The only downside is that it is less adopted, I will give you a snippet on how to load a template like that shortly.
Evgeny's avatar Evgeny (2011-07-24 14:54:24 -0500) edit
great :) I figured it was a jinja issue, but I can't figure out how to access jinja. I've updated the custom faq setting for template tags, and would like to complete it for trans tags as well.
NoahY's avatar NoahY (2011-07-24 14:57:16 -0500) edit
Very cool, I will take a look at your work tomorrow and probably merge.
Evgeny's avatar Evgeny (2011-07-25 00:18:46 -0500) edit
add a comment see more comments
1

Ooooooooo!

I got it! Sorry for being so dense. Here is the code that works:

def render_text(text, data, request):
    context = RequestContext(request, data)
    skin = get_skin(request)
    template = skin.from_string(text)
    return template.render(context)

This works with both {{settings.SETTING}} and {% trans %} foo {% endtrans %}

Yay :)

NoahY's avatar
303
NoahY
answered 2011-07-25 10:37:21 -0500
edit flag offensive 0 remove flag delete link

Comments

pushed to github
NoahY's avatar NoahY (2011-07-25 10:40:47 -0500) edit
add a comment see more comments
0

In askbot, because of the skins module, you need to do this:

from askbot.skins.loaders import get_template
from django.template import Context
template = get_template('some_template.txt')
output = template.render(Context({'some_data': 'some_value'}))

Here are docs for Jinja2. Jinja2 templating engine is used throughout askbot for the speed.

Evgeny's avatar
13.2k
Evgeny
updated 2011-07-24 15:07:27 -0500, answered 2011-07-24 15:04:40 -0500
edit flag offensive 0 remove flag delete link

Comments

The problem with that is it's a text file... I'm trying to turn a string into a template (an askbot setting). I can do it fine with `template = Template(text)`, it just doesn't like `trans` tags.
NoahY's avatar NoahY (2011-07-24 15:12:16 -0500) edit
Then you'll need to use "from jinja2 import Template" and then the same code that you used.
Evgeny's avatar Evgeny (2011-07-24 15:14:05 -0500) edit
File extension does not matter in the templates, but for strings there is Template class within jinja2.
Evgeny's avatar Evgeny (2011-07-24 15:15:44 -0500) edit
I tried that too :) it still says "unknown tag: trans"
NoahY's avatar NoahY (2011-07-24 15:39:53 -0500) edit
Even worse, it says "dictionary update sequence element #0 has length 1; 2 is required" even without the trans tags.
NoahY's avatar NoahY (2011-07-24 15:42:57 -0500) edit
add a comment see more comments
0

Okay, this isn't an answer, this is a continuation of the above comments, but I need room to post code :)

Here's my code, at the end of loaders.py:

def render_text(text, data, request):
    env = Environment(extensions=['jinja2.ext.i18n'])
    template = env.from_string(text)
    context = RequestContext(request, data)
    return shortcuts.render_to_string(template, context)

and it returns an error:

dictionary update sequence element #0 has length 1; 2 is required
NoahY's avatar
303
NoahY
updated 2011-07-25 10:20:05 -0500, answered 2011-07-25 09:58:39 -0500
edit flag offensive 0 remove flag delete link

Comments

In cases like this you can insert lines "import pdb" and "pdb.set_trace()" just before the failure point and try different solutions right in the python shell prompt. Looks like a plain python runtime error. You can hit "s" in the debugger and see exactly where the code breaks. Also - for debugging Jinja2 set DEBUG = True, TEMPLATE_DEBUG = False in the settings.py - it is weird but this is the way it works.
Evgeny's avatar Evgeny (2011-07-25 10:23:53 -0500) edit
Yeah, the latter is what I've been doing... I've never used the python shell, but it sounds helpful :)
NoahY's avatar NoahY (2011-07-25 10:41:41 -0500) edit
add a comment see more comments