Forked django-mako into our project.
This commit is contained in:
15
mitxmako/README
Normal file
15
mitxmako/README
Normal file
@@ -0,0 +1,15 @@
|
||||
================================================================================
|
||||
django-mako
|
||||
================================================================================
|
||||
This module provides a drop in replacement of Django templates for Mako
|
||||
Templates.
|
||||
|
||||
Django: http://www.djangoproject.com/
|
||||
Mako: http://www.makotemplates.org/
|
||||
|
||||
================================================================================
|
||||
How to install?
|
||||
================================================================================
|
||||
|
||||
$ sudo python setup.py install
|
||||
|
||||
16
mitxmako/__init__.py
Normal file
16
mitxmako/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# Copyright (c) 2008 Mikeal Rogers
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
lookup = None
|
||||
|
||||
41
mitxmako/middleware.py
Normal file
41
mitxmako/middleware.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# Copyright (c) 2008 Mikeal Rogers
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from mako.lookup import TemplateLookup
|
||||
import tempfile
|
||||
|
||||
class MakoMiddleware(object):
|
||||
def __init__(self):
|
||||
"""Setup mako variables and lookup object"""
|
||||
from django.conf import settings
|
||||
# Set all mako variables based on django settings
|
||||
global template_dirs, output_encoding, module_directory, encoding_errors
|
||||
directories = getattr(settings, 'MAKO_TEMPLATE_DIRS', settings.TEMPLATE_DIRS)
|
||||
|
||||
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
|
||||
if module_directory is None:
|
||||
module_directory = tempfile.mkdtemp()
|
||||
|
||||
output_encoding = getattr(settings, 'MAKO_OUTPUT_ENCODING', 'utf-8')
|
||||
encoding_errors = getattr(settings, 'MAKO_ENCODING_ERRORS', 'replace')
|
||||
|
||||
global lookup
|
||||
lookup = TemplateLookup(directories=directories,
|
||||
module_directory=module_directory,
|
||||
output_encoding=output_encoding,
|
||||
encoding_errors=encoding_errors,
|
||||
)
|
||||
import mitxmako
|
||||
mitxmako.lookup = lookup
|
||||
|
||||
37
mitxmako/shortcuts.py
Normal file
37
mitxmako/shortcuts.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2008 Mikeal Rogers
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from django.template import Context
|
||||
from django.http import HttpResponse
|
||||
|
||||
import middleware
|
||||
|
||||
def render_to_string(template_name, dictionary, context_instance=None):
|
||||
context_instance = context_instance or Context(dictionary)
|
||||
# add dictionary to context_instance
|
||||
context_instance.update(dictionary or {})
|
||||
# collapse context_instance to a single dictionary for mako
|
||||
context_dictionary = {}
|
||||
for d in context_instance:
|
||||
context_dictionary.update(d)
|
||||
# fetch and render template
|
||||
template = middleware.lookup.get_template(template_name)
|
||||
return template.render(**context_dictionary)
|
||||
|
||||
def render_to_response(template_name, dictionary, context_instance=None, **kwargs):
|
||||
"""
|
||||
Returns a HttpResponse whose content is filled with the result of calling
|
||||
lookup.get_template(args[0]).render with the passed arguments.
|
||||
"""
|
||||
return HttpResponse(render_to_string(template_name, dictionary, context_instance), **kwargs)
|
||||
28
mitxmako/template.py
Normal file
28
mitxmako/template.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# Copyright (c) 2008 Mikeal Rogers
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from mako.template import Template as MakoTemplate
|
||||
|
||||
import middleware
|
||||
|
||||
django_variables = ['lookup', 'template_dirs', 'output_encoding',
|
||||
'module_directory', 'encoding_errors',]
|
||||
|
||||
class Template(MakoTemplate):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Overrides base __init__ to provide django variable overrides"""
|
||||
if not kwargs.get('no_django', False):
|
||||
overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables])
|
||||
kwargs.update(overrides)
|
||||
super(Template, self).__init__(*args, **kwargs)
|
||||
Reference in New Issue
Block a user