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.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
NoahY's avatar
303
NoahY
asked 13 years ago

Comments

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.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
NoahY's avatar
303
NoahY
answered 13 years ago
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 (13 years ago)
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 (13 years ago)
Very cool, I will take a look at your work tomorrow and probably merge.
Evgeny's avatar Evgeny (13 years ago)
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 :)

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
NoahY's avatar
303
NoahY
answered 13 years ago
link

Comments

pushed to github
NoahY's avatar NoahY (13 years ago)
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.

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
Evgeny's avatar
13.2k
Evgeny
updated 13 years ago, answered 13 years ago
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 (13 years ago)
Then you'll need to use "from jinja2 import Template" and then the same code that you used.
Evgeny's avatar Evgeny (13 years ago)
File extension does not matter in the templates, but for strings there is Template class within jinja2.
Evgeny's avatar Evgeny (13 years ago)
I tried that too :) it still says "unknown tag: trans"
NoahY's avatar NoahY (13 years ago)
Even worse, it says "dictionary update sequence element #0 has length 1; 2 is required" even without the trans tags.
NoahY's avatar NoahY (13 years ago)
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

To enter a block of code:

  • enter empty line after your previous text
  • paste or type the code
  • select the code and press the button above
Preview: (hide)
NoahY's avatar
303
NoahY
updated 13 years ago, answered 13 years ago
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 (13 years ago)
Yeah, the latter is what I've been doing... I've never used the python shell, but it sounds helpful :)
NoahY's avatar NoahY (13 years ago)
see more comments