Ask Your Question
1

How to perform Template() on askbot setting?

asked 2011-07-23 08:46:58 -0500

NoahY gravatar image

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.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
1

answered 2011-07-25 10:37:21 -0500

NoahY gravatar image

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 :)

edit flag offensive delete link more

Comments

pushed to github
NoahY gravatar imageNoahY ( 2011-07-25 10:40:47 -0500 )edit
1

answered 2011-07-24 14:52:06 -0500

NoahY gravatar image

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.

edit flag offensive delete link more

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 gravatar imageEvgeny ( 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 gravatar imageNoahY ( 2011-07-24 14:57:16 -0500 )edit
Very cool, I will take a look at your work tomorrow and probably merge.
Evgeny gravatar imageEvgeny ( 2011-07-25 00:18:46 -0500 )edit
0

answered 2011-07-24 15:04:40 -0500

Evgeny gravatar image

updated 2011-07-24 15:07:27 -0500

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.

edit flag offensive delete link more

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 gravatar imageNoahY ( 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 gravatar imageEvgeny ( 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 gravatar imageEvgeny ( 2011-07-24 15:15:44 -0500 )edit
I tried that too :) it still says "unknown tag: trans"
NoahY gravatar imageNoahY ( 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 gravatar imageNoahY ( 2011-07-24 15:42:57 -0500 )edit
0

answered 2011-07-25 09:58:39 -0500

NoahY gravatar image

updated 2011-07-25 10:20:05 -0500

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
edit flag offensive delete link more

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 gravatar imageEvgeny ( 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 gravatar imageNoahY ( 2011-07-25 10:41:41 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-07-23 08:46:58 -0500

Seen: 592 times

Last updated: Jul 25 '11