Login

themed_template_loader

Author:
amitu
Posted:
March 2, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

I developed this template loader for adding themes support in gitology. In order to support theming django applications, add this template loader at as the first TEMPLATE_LOADERS settings.py setting.

Anywhere you request base.html, blog/index.html, when the theme is set to "bw", it will look for bw/base.html or bw/blog/index.html files first.

Takes care of both render_to_response() in view or {% load template %} in templates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# based on django.templates.loaders.filesystem
# imports # {{{
from django.conf import settings
from django.template import TemplateDoesNotExist
from django.utils._os import safe_join
 
from gitology.config import settings as gsettings
# }}}
 
# get_template_sources # {{{
def get_template_sources(template_name, template_dirs=None):
    if not hasattr(gsettings.threadlocal, "theme"):
        print "get_template_sources no threadlocal"
        return
    theme = gsettings.threadlocal.theme + "/"
    if not template_dirs:
        template_dirs = settings.TEMPLATE_DIRS
    for template_dir in template_dirs:
        try:
            yield safe_join(template_dir, theme + template_name)
        except ValueError:
            # The joined path was located outside of template_dir.
            pass
# }}}
 
# load_template_source # {{{
def load_template_source(template_name, template_dirs=None):
    tried = []
    for filepath in get_template_sources(template_name, template_dirs):
        try:
            return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
        except IOError:
            tried.append(filepath)
    if tried:
        error_msg = "Tried %s" % tried
    else:
        error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
    raise TemplateDoesNotExist, error_msg
# }}}
 
load_template_source.is_usable = True

################################
# view decorator to pick theme #
################################

from django.conf import settings
import threading

settings.threadlocal = threading.local()

# select_theme # {{{
def select_theme(view_func):
    def wrapped(request, *args, **kw):
        if request.GET.get("theme"):
            settings.threadlocal.theme = request.GET["theme"]
        elif "THEME" in request.COOKIES:
            settings.threadlocal.theme = request.COOKIES["THEME"]
        elif "THEME" in request.session:
            settings.threadlocal.theme = request.session["THEME"]
        elif "THEME" in gsettings.DEFAULTS:
            settings.threadlocal.theme = settings.DEFAULT_THEME
        return view_func(request, *args, **kw)
    wrapped.__doc__ = view_func.__doc__
    wrapped.__dict__ = view_func.__dict__
    return wrapped
# }}}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

wancharle (on September 3, 2009):

Very Cool!

I thought about it, but I looked here and you had already done.

He also works with "inclusion tags?

#

ckarrie2 (on January 26, 2012):

gitology source moved to https://github.com/amitu/gitology

#

Please login first before commenting.