Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: benhoyt/symplate
base: b582b91153
...
head fork: benhoyt/symplate
compare: 7996138300
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 38 additions and 23 deletions.
  1. +18 −11 README.md
  2. +16 −12 symplate.py
  3. +4 −0 tests/test_renderer.py
View
29 README.md
@@ -93,6 +93,9 @@ Times are normalized to the HandCoded render time:
Cheetah 2.4.4 121.454 5.919
Django 1.3.3 7.698 20.506
+I ran these benchmarks on my Intel Core i5-2450 on Windows 7, running CPython
+2.7.3 64-bit.
+
Basic usage
-----------
@@ -448,23 +451,27 @@ To customize rendering settings, simply pass arguments to the `Renderer()`
initializer as follows:
* **template_dir** is the only required argument -- it specifies the root
-directory of your Symplate source files.
+ directory of your Symplate source files.
* **output_dir** is the directory the compiled Python template files will go
-into. The default is `symplouts` at the same level as your `template_dir`.
+ into. The default is `symplouts` at the same level as your `template_dir`.
* **extension** is the file extension for templates. The default is `'.symp'`.
-Set this to `''` if you want to specify the file extension explicitly when
-calling render.
+ Set this to `''` if you want to specify the file extension explicitly when
+ calling render.
* **check_mtimes** is off by default. Set to True to tell Symplate to check
-the template files' modify times on render, which is slower and usually only
-used for debugging.
+ the template files' modify times on render, which is slower and usually only
+ used for debugging.
+* **auto_compile**, which is on by default, means Symplate will automatically
+ compile templates to .py files when you call `render()`. Set to False if
+ you've deployed the compiled .py files along with your templates, or if
+ you've already called `compile_all()` manually.
* **modify_path** is on by default, and means Symplate will put
-`output_dir/..` first on `sys.path` so it can import compiled templates. Set
-to False if you want to manage this manually.
+ `output_dir/..` first on `sys.path` so it can import compiled templates. Set
+ to False if you want to manage this manually.
* **preamble** defaults to empty string, and specifies extra code to include
-at the top of all compiled template. Useful for imports you use in many
-templates.
+ at the top of all compiled template. Useful for imports you use in many
+ templates.
* **default_filter** defaults to `'symplate.html_filter'`, and is used to
-[override the default filter](#overriding-the-default-filter).
+ [override the default filter](#overriding-the-default-filter).
The public methods of `Renderer` instances are `render`, `compile`, and
`compile_all`, though often you'll only need `render`. You use these functions
View
28 symplate.py
@@ -75,8 +75,8 @@ class Renderer(object):
"""
def __init__(self, template_dir, output_dir=None, extension='.symp',
- check_mtimes=False, modify_path=True, preamble='',
- default_filter='symplate.html_filter'):
+ check_mtimes=False, auto_compile=True, modify_path=True,
+ preamble='', default_filter='symplate.html_filter'):
"""Initialize a Renderer instance. See README.md for more info."""
self.template_dir = os.path.abspath(template_dir)
if output_dir is None:
@@ -85,6 +85,7 @@ def __init__(self, template_dir, output_dir=None, extension='.symp',
self.output_dir = output_dir
self.extension = extension
self.check_mtimes = check_mtimes
+ self.auto_compile = auto_compile
self.preamble = preamble
self.default_filter = default_filter
@@ -363,16 +364,17 @@ def _get_module(self, name):
"""Import (or compile and import) named template and return module."""
names = self._get_filenames(name)
- # compile the template source to .py if it has changed
- try:
- py_mtime = os.path.getmtime(names['py'])
- except OSError:
- py_mtime = 0
- if os.path.getmtime(names['symplate']) > py_mtime:
- self.compile(name)
- # .py changed, ensure module is reloaded when imported below
- if names['module'] in sys.modules:
- sys.modules.pop(names['module'])
+ if self.auto_compile:
+ # compile the template source to .py if it has changed
+ try:
+ py_mtime = os.path.getmtime(names['py'])
+ except OSError:
+ py_mtime = 0
+ if os.path.getmtime(names['symplate']) > py_mtime:
+ self.compile(name)
+ # .py changed, ensure module is reloaded when imported below
+ if names['module'] in sys.modules:
+ sys.modules.pop(names['module'])
# try to import the compiled template; if it doesn't exist (it's never
# been compiled), compile it and then re-import
@@ -380,6 +382,8 @@ def _get_module(self, name):
module = __import__(names['module'], globals(), locals(),
[names['import']])
except ImportError:
+ if not self.auto_compile:
+ raise
self.compile(name)
module = __import__(names['module'], globals(), locals(),
[names['import']])
View
4 tests/test_renderer.py
@@ -36,6 +36,10 @@ def test_check_mtimes_false(self):
self.assertEquals(self.render('{% template %}cmf2', _renderer=renderer, _increment=0, _adjust_mtime=5), 'cmf2')
self.assertEquals(self.render('{% template %}cmf3', _renderer=renderer, _increment=0, _adjust_mtime=5), 'cmf2')
+ def test_auto_compile_false(self):
+ renderer = utils.Renderer(auto_compile=False)
+ self.assertRaises(ImportError, self.render, '{% template %}foo', _renderer=renderer)
+
def test_modify_path(self):
saved_path = list(sys.path)
try:

No commit comments for this range

Something went wrong with that request. Please try again.