django template wrapper helper

March 9, 2008 | In: development, django, python

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',})
Bookmark and Share
  • ninjacipher
    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).
  • Sorry about the tone of my comment. I just re-read it and it came across as glib, which was not my intention.
  • ninjacipher
    um cause i forgot about it... :)
  • Umm, why not just use render_to_string? ( http://www.djangoproject.com/documentation/temp... )
blog comments powered by Disqus

Flickr Pics

13th and washington NYC13th and 9th NYC1st st Jersey CityIMG_2813IMG_2814_2IMG_2816IMG_2815Photo_011908_001Photo_122807_001Photo_121107_001