You can download these files from here
Django template, mako, genshi, they are the best three templates in python, aren't they?
How to use these files ?
Using Mako in Django -- by John Leung
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | ###########
#common.py
###########
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
import os
app_dirs = []
for app in settings.INSTALLED_APPS:
i = app.rfind('.')
if i == -1:
m, a = app, None
else:
m, a = app[:i], app[i+1:]
try:
if a is None:
mod = __import__(m, {}, {}, [])
else:
mod = getattr(__import__(m, {}, {}, [a]), a)
except ImportError, e:
raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0])
app_dirs.append(os.path.dirname(mod.__file__))
#################
#mako_django.py
#################
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.template import TemplateDoesNotExist
from django.template.context import Context
from mako.lookup import TemplateLookup
from mako.template import Template
from mako.exceptions import TopLevelLookupException
import os
from common import app_dirs
'''
configurations:
MAKO_TEMPLATE_DIRS:
A tuple, specify the directories in which to find the mako templates,
just like TEMPLATE_DIRS .
default value is ('mako_templates',)
MAKO_MODULE_DIR:
A string, if specified, all of the compiled template module files will be
stored in this directory.
MAKO_MODULENAME_CALLABLE:
A callable, if MAKO_MODULE_DIR is not specified, this will be
used to determine the filename of compiled template module file.
See [http://www.makotemplates.org/trac/ticket/14]
Default to the function `default_module_name`, which
just appends '.py' to the template filename.
'''
app_template_dirs = []
for app_dir in app_dirs:
template_dir = os.path.join(app_dir, 'mako_templates')
if os.path.isdir(template_dir):
app_template_dirs.append(template_dir)
template_dirs = getattr(settings, 'MAKO_TEMPLATE_DIRS', None) or ('mako_templates',)
template_dirs += tuple(app_template_dirs)
def default_module_name(filename, uri):
'''
Will store module files in the same directory as the corresponding template files.
detail about module_name_callable, go to
http://www.makotemplates.org/trac/ticket/14
'''
return filename+'.py'
module_dir = getattr(settings, 'MAKO_MODULE_DIR', None)
if module_dir:
lookup = TemplateLookup(directories=template_dirs,
module_directory=module_dir)
else:
module_name_callable = getattr(settings, 'MAKO_MODULENAME_CALLABLE', None)
if callable(module_name_callable):
lookup = TemplateLookup(directories=template_dirs,
modulename_callable=module_name_callable)
else:
lookup = TemplateLookup(directories=template_dirs,
modulename_callable=default_module_name)
def select_template(template_name_list):
for template_name in template_name_list:
try:
return lookup.get_template(template_name)
except TopLevelLookupException:
pass
raise TemplateDoesNotExist, 'mako templates: '+', '.join(template_name_list)
def get_template(template_name):
try:
return lookup.get_template(template_name)
except TopLevelLookupException:
raise TemplateDoesNotExist, 'mako templates: '+template_name
def render_to_response(template_name, dictionary=None,
context_instance=None):
if isinstance(template_name, (list, tuple)):
template = select_template(template_name)
else:
template = get_template(template_name)
dictionary = dictionary or {}
if context_instance is None:
context_instance = Context(dictionary)
else:
context_instance.update(dictionary)
data = {}
[data.update(d) for d in context_instance]
return HttpResponse(template.render(**data))
##################
#genshi_django.py
##################
from django.conf import settings
from django.http import HttpResponse
from django.template import TemplateDoesNotExist
from django.template.context import Context
from genshi.template import MarkupTemplate, TemplateLoader
from genshi.template.loader import TemplateNotFound
import os
from common import app_dirs
'''
configuration:
GENSHI_TEMPLATE_DIRS:
specify directories in which to find the genshi template files.
default value is ('genshi_templates',)
'''
app_template_dirs = []
for app_dir in app_dirs:
template_dir = os.path.join(app_dir, 'genshi_templates')
if os.path.isdir(template_dir):
app_template_dirs.append(template_dir)
template_dirs = getattr(settings, 'GENSHI_TEMPLATE_DIRS', None) or ('genshi_templates',)
template_dirs += tuple(app_template_dirs)
loader = TemplateLoader(template_dirs, auto_reload=settings.DEBUG)
def select_template(template_name_list):
for template_name in template_name_list:
try:
return loader.load(template_name)
except TemplateNotFound:
pass
raise TemplateDoesNotExist, 'genshi templates: '+', '.join(template_name_list)
def get_template(template_name):
try:
return loader.load(template_name)
except TemplateNotFound:
raise TemplateDoesNotExist, 'genshi templates: '+template_name
def render_to_response(template_name, dictionary=None,
context_instance=None):
if isinstance(template_name, (list, tuple)):
template = select_template(template_name)
else:
template = get_template(template_name)
dictionary = dictionary or {}
if context_instance is None:
context_instance = Context(dictionary)
else:
context_instance.update(dictionary)
data = {}
[data.update(d) for d in context_instance]
stream = template.generate(**data)
return HttpResponse(stream.render('xhtml'))
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Hello! Nice code, but if I may allow a suggestion: the mako TemplateLookups should allow to specify default_filters if the data is non-ascii (I use default_filters=['decode.utf_8'] ); and similarly, the mako template.render should be template.render_unicode(**data).encode('utf-8', 'replace')
Cheers
#
Please login first before commenting.