Commit c99bc566 by Piotr Mitros

Forked django-mako into our project.

parent 9d4c91e4
================================================================================
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
# 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
# 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
# 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)
# 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment