django template wrapper helper
Sunday, March 9th, 2008edit:
# 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',})