themed_template_loader

 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. Show template names in markup by jaywhy13 1 year, 10 months ago
  2. View decorator to automate templates. by jafo 4 years, 2 months ago
  3. Load Template from App by mikob 4 months, 3 weeks ago
  4. Template loader to target a specific template by miracle2k 2 years, 9 months ago
  5. Language aware template loader by rmt 5 years 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

#

(Forgotten your password?)