Writing your own template loaders

By : Shabda Raaj

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

  1. Returns a tuple of (openfile, filename) if it can find the template.
  2. 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 .


Related Posts


Can we help you build amazing apps? Contact us today.

Comments

Alex Gaynor

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.

commmenttor
charlie 22nd Nov., 2009

So, is there any real reason why I would like to create my own template loader?

commmenttor
hgreg 22nd Nov., 2009

i'm with charlie…what's the point of this?

commmenttor
shabda

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.

commmenttor
kevin

as always, enjoying the posts. thanks.

commmenttor
test

I'm not that much of a online reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back in the future. All the best

commmenttor
ugg ムートン

China Construction Bank edged up 0.41 percent to 4.94 yuan. China Merchants Bank rose 2.09 percent to 13.67 yuan. Industrial and Commercial Bank of China edged down 0.7 percent to 4.24 yuan. Bank of China was unchanged at 3.55 yuan.

commmenttor
air max ones

Navigate To THIS Site

commmenttor
Post a comment Name :

Email :

Your site url:

Comment :

© Agiliq, 2009-2012