July 21st, 2006 posted by Bender Rodríguez
Part II in series of articles to document the trials and tribulations of developing a content management system that draws its templates from a database using django
snippet = Snippet.objects.get(name=self.name)
t = template.Template(snippet.content)
context[self.varname] = t.render(context)
will render any template code and variables properly and spit out text/html but also provides access to essential template objects via the auth context processor. and because we have some sitewide variables declared in a custom context processor that are then available in the templates, we can also make use of them as well when called from a templatetag that draws its data from a database. sweet.
one side note about the {% load templatetag %} manoeuvre, because django templates are not designed to inherit the load values from parents, you have to include the load command in each page part and snippet, which is kind of a drag but that's how django works. as the authors state in the [documentation](http://www.djangoproject.com/documentation/templates/):
"When you load a custom tag or filter library, the tags/filters are only made available to the current template -- not any parent or child templates along the template-inheritance path.
"For example, if a template foo.html has {% load comments %}, a child template (e.g., one that has {% extends "foo.html" %}) will not have access to the comments template tags and filters. The child template is responsible for its own {% load comments %}.
"This is a feature for the sake of maintainability and sanity."
0 Comments,
0 trackbacks
(Trackback URL)
"God must have loved the people in power, for he made them so much like their own image of him."
Kenneth Patchen