We love designing and developing websites, but what really drives us is solving problems and cultivating strong relationships with our clients
Writing your own template loaders
By : shabda
Django has three builtin template loaders which are used to get the templates for rendering.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
Writing your template loader is a awfuly easy. It is a callable which
- Returns a tuple of (openfile, filename) if it can find the template.
- Raise TemplateDoesNotExist if the templates cannot be found.
The simplest template loader can be
from django.template import TemplateDoesNotExist
def load_template_source(template_name, template_dirs=None):
try:
return open(template_name).read(), template_name
except IOError:
raise TemplateDoesNotExist, template_name
if you put this to your template loaders directory,
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
'dgrid.load_template_source'
# 'django.template.loaders.eggs.load_template_source',
You can do access a template using an absolute path.
In [6]: get_template('/home/shabda/abc.txt')
Out[6]: <django.template.Template object at 0x91c860c>
In [7]: temp = get_template('/home/shabda/abc.txt')
In [8]: temp
Out[8]: <django.template.Template object at 0x9358a0c>
In [9]: temp.render
Out[9]: <bound method Template.render of <django.template.Template object at 0x9358a0c>>
This is the first in the series of short and sweet Django posts we are going to do. You are interested, right. Do follow us on twitter or subscribe to our feed.
We build Amazing We Apps. Talk to us or email us at sales@uswaretech.com .
Comments
So, is there any real reason why I would like to create my own template loader?
Yes, for example, if you are using app_directories.load_template_source, then your template structures look like,
app1/templates/app1/template.html
app2/templates/app2/template.html
While I would like them in
app1/templates/template.html
app2/templates/template.html
This can be done via writig your own template loaders.
- How to use pep8.py to write better Django code
- Screencast: Django Tutorial Part 1
- How and why to use pyflakes to write better Python
- Getting started with South for Django DB migrations
- A brief overview of Vagrant
- Writing jQuery plugins using Coffeescript
- Behind the Scenes: Request to Response
- Using SQLite Database with Android
- Haml for Django developers
- Coffeescript for Python programmers
- rails
- django
- linkroundup
- django opinion
- opinion
- business
- API
- appengine
- python
- satire
- startup
- Uncategorized
- marketing
- personal
- rambling
- search
- interviews
- seo-interviews
- 5startupideas
- ideas
- seo
- tips
- forms
- paypal
- utilities
- datetime
- web2.0
- Amazon
- algorithms
- presentations
- products
- pinax
- satchmo
- ecommerce
- microsoft
- yahoo
- book
- tutorial
- models
- aggreagtion
- meta
- India
- apps
- about
- CSS
- Design
- wordpress
- test slug
- vim
- urls
- reviews
- javascript
- xmpp
- emacs
- Typography
- Grid Theory
- Color Theory
- iphone
- android
- titanium
- mobile applications
- CSS3
- Browser Compatibility
- mobile
- jobs
- lamson
- django setup
- files
- upload
- jsTree
- hierarchical view
- web page
- Treeview
- coffeescript
- request
- response
- South
- django south
- django migration
- --fake
- screencasts
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- June 2010
- April 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- April 2009
- March 2009
- February 2009
- November 2008
- October 2008
- June 2008
- May 2008
- April 2008
Fair warning, the template loader API is going to be changing in the coming weeks as a part of ticket #6262, old ones will continue to work though.