summaryrefslogtreecommitdiff
path: root/pystache/renderer.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2011-12-27 18:46:14 -0800
committerChris Jerdonek <chris.jerdonek@gmail.com>2011-12-27 18:46:14 -0800
commit98e4f43ff7e5441a113127c6427dbaa75530c54c (patch)
treeeddda0213632ab9f7a34b29f7e6dd92f9097a63f /pystache/renderer.py
parent4720fa7f65ac6062ec43ec3889fd9fc401a08df1 (diff)
downloadpystache-98e4f43ff7e5441a113127c6427dbaa75530c54c.tar.gz
The View class now does all its loading through a Renderer instance.
This will help with future refactoring and code maintenance by not having the same logic duplicated in more than one part of the code. It also takes more responsibility away from the View class, which is what we want.
Diffstat (limited to 'pystache/renderer.py')
-rw-r--r--pystache/renderer.py39
1 files changed, 34 insertions, 5 deletions
diff --git a/pystache/renderer.py b/pystache/renderer.py
index 0323800..72aed73 100644
--- a/pystache/renderer.py
+++ b/pystache/renderer.py
@@ -6,9 +6,11 @@ This module provides a Renderer class to render templates.
"""
import cgi
+import os
import sys
from .context import Context
+from .loader import DEFAULT_EXTENSION
from .loader import Loader
from .reader import Reader
from .renderengine import RenderEngine
@@ -34,7 +36,8 @@ class Renderer(object):
"""
def __init__(self, loader=None, file_encoding=None, default_encoding=None,
- decode_errors='strict', escape=None):
+ decode_errors='strict', search_dirs=None, file_extension=None,
+ escape=None):
"""
Construct an instance.
@@ -80,28 +83,54 @@ class Renderer(object):
strings of type str encountered during the rendering process.
Defaults to "strict".
+ search_dirs: the list of directories in which to search for
+ templates when loading a template by name. Defaults to the
+ current working directory. If given a string, the string is
+ interpreted as a single directory.
+
+ file_extension: the template file extension. Defaults to "mustache".
+ Pass False for no extension (i.e. for extensionless files).
+
"""
if default_encoding is None:
default_encoding = sys.getdefaultencoding()
- if file_encoding is None:
- file_encoding = default_encoding
-
if escape is None:
# The quote=True argument causes double quotes to be escaped,
# but not single quotes:
# http://docs.python.org/library/cgi.html#cgi.escape
escape = lambda s: cgi.escape(s, quote=True)
+ # This needs to be after we set the default default_encoding.
+ if file_encoding is None:
+ file_encoding = default_encoding
+
+ if file_extension is None:
+ file_extension = DEFAULT_EXTENSION
+
+ if search_dirs is None:
+ search_dirs = os.curdir # i.e. "."
+
+ if isinstance(search_dirs, basestring):
+ search_dirs = [search_dirs]
+
+ # This needs to be after we set some of the defaults above.
if loader is None:
reader = Reader(encoding=file_encoding, decode_errors=decode_errors)
- loader = Loader(reader=reader)
+ loader = Loader(reader=reader, search_dirs=search_dirs, extension=file_extension)
self.decode_errors = decode_errors
self.default_encoding = default_encoding
self.escape = escape
self.file_encoding = file_encoding
+ self.file_extension = file_extension
+ # TODO: we should not store a loader attribute because the loader
+ # would no longer reflect the current attributes if, say, someone
+ # changed the search_dirs attribute after instantiation. Instead,
+ # we should construct the Loader instance each time on the fly,
+ # as we do with the Reader in the read() method.
self.loader = loader
+ self.search_dirs = search_dirs
def _to_unicode_soft(self, s):
"""