NinjaCipher:-*-:ro60

django template wrapper helper

edit:
# ericflo pointed out something that I totally forgot about… render_to_string. And he’s completely correct… It does exactly the same thing and most likely does it better… DOH! Well whatever… here is my version anyway ;)

—————————
Here is a handy little helper I cooked up for dynamically loading dynamic content into a reusable template wrapper. It takes the path of a template and a dict of vars to populate, compiles the template and returns the resulting value as a string. You can then pass in the resulting variable into your main context. Pretty simple but very useful.

def wrap_content(template, var_dict):
    from django.template import Context, loader
    c = Context(var_dict)
    t = loader.get_template(template)
    return t.render(c)

called like so

mod = wrap_content('shared/mod_wrapper.html',{'title':'test title','content':'content test',})
Del.icio.us Digg BlinkList Furl Ma.gnolia Reddit Spurl

4 Responses to “django template wrapper helper”

  1. ericflo Says:

    Umm, why not just use render_to_string? ( http://www.djangoproject.com/documentation/templates_python/#the-render-to-string-shortcut )

  2. mattd Says:

    um cause i forgot about it… :)

  3. ericflo Says:

    Sorry about the tone of my comment. I just re-read it and it came across as glib, which was not my intention.

  4. mattd Says:

    It’s all good, you were completely correct. I’m just glad someone is paying attention (even if it’s to tell me I’m missing the plot).

Leave a Reply

You must be logged in to post a comment.