Permalink
Browse files

Fix for Trac ticket:2192

  • Loading branch information...
1 parent 0980400 commit 30df9a74d9efb29a23ad6899754e8bfd996bb96b @chbrown chbrown committed Jan 23, 2010
Showing with 38 additions and 47 deletions.
  1. +2 −0 .hgignore
  2. +27 −33 tg/configuration.py
  3. +1 −3 tg/dottednamesupport.py
  4. +6 −8 tg/render.py
  5. +2 −3 tg/tests/test_configuration.py
View
@@ -9,6 +9,7 @@ syntax: glob
.coverage
.noseids
*.egg
+*.DS_Store
syntax: regexp
^build
@@ -26,3 +27,4 @@ syntax: regexp
^SQL
^Turbo
^zope
+
View
@@ -135,14 +135,6 @@ def __init__(self):
# this is to activate the legacy renderers
# legacy renderers are buffet interface plugins
self.use_legacy_renderer = False
- # if this is set to True the @expose decorator will be able to
- # specify template names using a dotted name that will be searched
- # in the python path. This option is used in tg.render.render_genshi
- # TODO: we should set this to False once we implement simple names
- # support in the @expose decorator as explained in #1942
- # for the moment only the dotted names notation is supported with the
- # new generation renderer functions
- self.use_dotted_templatenames = True
self.use_toscawidgets = True
self.use_transaction_manager = True
@@ -305,7 +297,7 @@ def setup_sa_auth_backend(self):
config['sa_auth'] = defaults
config['sa_auth'].update(self.sa_auth)
- def setup_mako_renderer(self):
+ def setup_mako_renderer(self, use_dotted_templatenames=None):
"""Setup a renderer and loader for mako templates.
Override this to customize the way that the mako template
@@ -318,44 +310,46 @@ def setup_mako_renderer(self):
provided by tg.render.
"""
- from tg.dottednamesupport import DottedTemplateLookup
- from mako.lookup import TemplateLookup
-
+
from tg.render import render_mako
+
+ if not use_dotted_templatenames:
+ use_dotted_templatenames = asbool(config.get('use_dotted_templatenames', 'true'))
-
- if config.get('use_dotted_templatenames', False):
+ if use_dotted_templatenames:
# Support dotted names by injecting a slightly different template
- # lookup system that will return templates from dotted template
- # notation.
+ # lookup system that will return templates from dotted template notation.
+ from tg.dottednamesupport import DottedTemplateLookup
config['pylons.app_globals'].mako_lookup = DottedTemplateLookup(
input_encoding='utf-8', output_encoding='utf-8',
imports=['from webhelpers.html import escape'],
default_filters=['escape'])
else:
- compiled_dir = tg.config.get('templating.mako.compiled_templates_dir', None)
-
- if not compiled_dir:
- # no specific compile dir give by conf... we expect that
- # the server will have access to the first template dir
- # to write the compiled version...
- # If this is not the case we are doomed and the user should
- # provide us the required config...
- compiled_dir = self.paths['templates'][0]
-
# If no dotted names support was required we will just setup
# a file system based template lookup mechanism.
compiled_dir = tg.config.get('templating.mako.compiled_templates_dir', None)
if not compiled_dir:
- # no specific compile dir give by conf... we expect that
- # the server will have access to the first template dir
- # to write the compiled version...
- # If this is not the case we are doomed and the user should
- # provide us the required config...
- compiled_dir = self.paths['templates'][0]
-
+ # Try each given templates path (when are they > 1 ?) for writability..
+ for template_path in self.paths['templates']:
+ if os.access(template_path, os.W_OK):
+ compiled_dir = template_path
+ break # first match is as good as any
+
+ # Last recourse: project-dir/data/templates (pylons' default directory)
+ if not compiled_dir:
+ root = os.path.dirname(os.path.abspath(self.package.__file__))
+ pylons_default_path = os.path.join(root, '../data/templates')
+ if os.access(pylons_default_path, os.W_OK):
+ compiled_dir = pylons_default_path
+
+ if not compiled_dir:
+ raise IOError("None of your templates directory, %s, are writable for compiled "
+ "templates. Please set the templating.mako.compiled_templates_dir "
+ "variable in your .ini file" % str(self.paths['templates']))
+
+ from mako.lookup import TemplateLookup
config['pylons.app_globals'].mako_lookup = TemplateLookup(
directories=self.paths['templates'],
module_directory=compiled_dir,
View
@@ -75,8 +75,7 @@ def adjust_uri(self, uri, relativeto):
# method is called by template inheritance we must
# support dotted names also in the inheritance.
result = tg.config['pylons.app_globals'].\
- dotted_filename_finder.get_dotted_filename(template_name=uri,
- template_extension='.mak')
+ dotted_filename_finder.get_dotted_filename(template_name=uri, template_extension='.mak')
if not self.template_filenames_cache.has_key(uri):
# feed our filename cache if needed.
@@ -169,7 +168,6 @@ def get_template(self, template_name):
# Do so now
self.__load(template_name)
- #TODO: use the paste asbool function here.
if asbool(tg.config.get('templating.mako.reloadfromdisk', 'false')):
# AUTO RELOADING will be activated only if user has
# explicitly asked for it in the configuration
View
@@ -2,6 +2,7 @@
from genshi import HTML, XML
from pylons.configuration import config
+from paste.deploy.converters import asbool
from pylons import (app_globals, session, tmpl_context, request,
response, templating)
from repoze.what import predicates
@@ -133,7 +134,7 @@ def render(template_vars, template_engine=None, template_name=None, **kwargs):
if not template_vars:
template_vars={}
- if template_engine != "json" and template_engine!= 'amf':
+ if template_engine != "json" and template_engine != 'amf':
#Get the extra vars, and merge in the vars from the controller
tg_vars = _get_tg_vars()
tg_vars.update(template_vars)
@@ -172,14 +173,11 @@ def render_genshi(template_name, template_vars, **kwargs):
def render_mako(template_name, template_vars, **kwargs):
- if config.get('use_dotted_templatenames', False):
- template_name = tg.config['pylons.app_globals'
- ].dotted_filename_finder.get_dotted_filename(
- template_name,
- template_extension='.mak')
+ if asbool(config.get('use_dotted_templatenames', 'true')):
+ template_name = tg.config['pylons.app_globals'].\
+ dotted_filename_finder.get_dotted_filename(template_name, template_extension='.mak')
- return templating.render_mako(template_name, extra_vars=template_vars,
- **kwargs)
+ return templating.render_mako(template_name, extra_vars=template_vars, **kwargs)
def render_jinja(template_name, template_vars, **kwargs):
return templating.render_jinja2(template_name, extra_vars=template_vars,
@@ -98,9 +98,8 @@ def test_setup_jinja_renderer(self):
self.config.setup_jinja_renderer()
def test_setup_mako_renderer(self):
- self.config.paths.templates = 'template_path'
- self.config.setup_mako_renderer()
-
+ self.config.paths.templates = ['template_path']
+ self.config.setup_mako_renderer(use_dotted_templatenames=True)
def test_setup_sqlalchemy(self):
config['sqlalchemy.url'] = 'sqlite://'

0 comments on commit 30df9a7

Please sign in to comment.