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',})

March 9th, 2008 at 8:50 pm
Umm, why not just use render_to_string? ( http://www.djangoproject.com/documentation/templates_python/#the-render-to-string-shortcut )
March 9th, 2008 at 9:27 pm
um cause i forgot about it…
March 12th, 2008 at 6:09 am
Sorry about the tone of my comment. I just re-read it and it came across as glib, which was not my intention.
March 12th, 2008 at 8:20 am
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).