Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor

Showing 3 changed files with 38 additions and 23 deletions. Show diff stats Hide diff stats

  1. +18 11 README.md
  2. +16 12 symplate.py
  3. +4 0 tests/test_renderer.py
29 README.md
Source Rendered
@@ -93,6 +93,9 @@ Times are normalized to the HandCoded render time:
93 93 Cheetah 2.4.4 121.454 5.919
94 94 Django 1.3.3 7.698 20.506
95 95
  96 +I ran these benchmarks on my Intel Core i5-2450 on Windows 7, running CPython
  97 +2.7.3 64-bit.
  98 +
96 99
97 100 Basic usage
98 101 -----------
@@ -448,23 +451,27 @@ To customize rendering settings, simply pass arguments to the `Renderer()`
448 451 initializer as follows:
449 452
450 453 * **template_dir** is the only required argument -- it specifies the root
451   -directory of your Symplate source files.
  454 + directory of your Symplate source files.
452 455 * **output_dir** is the directory the compiled Python template files will go
453   -into. The default is `symplouts` at the same level as your `template_dir`.
  456 + into. The default is `symplouts` at the same level as your `template_dir`.
454 457 * **extension** is the file extension for templates. The default is `'.symp'`.
455   -Set this to `''` if you want to specify the file extension explicitly when
456   -calling render.
  458 + Set this to `''` if you want to specify the file extension explicitly when
  459 + calling render.
457 460 * **check_mtimes** is off by default. Set to True to tell Symplate to check
458   -the template files' modify times on render, which is slower and usually only
459   -used for debugging.
  461 + the template files' modify times on render, which is slower and usually only
  462 + used for debugging.
  463 +* **auto_compile**, which is on by default, means Symplate will automatically
  464 + compile templates to .py files when you call `render()`. Set to False if
  465 + you've deployed the compiled .py files along with your templates, or if
  466 + you've already called `compile_all()` manually.
460 467 * **modify_path** is on by default, and means Symplate will put
461   -`output_dir/..` first on `sys.path` so it can import compiled templates. Set
462   -to False if you want to manage this manually.
  468 + `output_dir/..` first on `sys.path` so it can import compiled templates. Set
  469 + to False if you want to manage this manually.
463 470 * **preamble** defaults to empty string, and specifies extra code to include
464   -at the top of all compiled template. Useful for imports you use in many
465   -templates.
  471 + at the top of all compiled template. Useful for imports you use in many
  472 + templates.
466 473 * **default_filter** defaults to `'symplate.html_filter'`, and is used to
467   -[override the default filter](#overriding-the-default-filter).
  474 + [override the default filter](#overriding-the-default-filter).
468 475
469 476 The public methods of `Renderer` instances are `render`, `compile`, and
470 477 `compile_all`, though often you'll only need `render`. You use these functions
28 symplate.py
@@ -75,8 +75,8 @@ class Renderer(object):
75 75 """
76 76
77 77 def __init__(self, template_dir, output_dir=None, extension='.symp',
78   - check_mtimes=False, modify_path=True, preamble='',
79   - default_filter='symplate.html_filter'):
  78 + check_mtimes=False, auto_compile=True, modify_path=True,
  79 + preamble='', default_filter='symplate.html_filter'):
80 80 """Initialize a Renderer instance. See README.md for more info."""
81 81 self.template_dir = os.path.abspath(template_dir)
82 82 if output_dir is None:
@@ -85,6 +85,7 @@ def __init__(self, template_dir, output_dir=None, extension='.symp',
85 85 self.output_dir = output_dir
86 86 self.extension = extension
87 87 self.check_mtimes = check_mtimes
  88 + self.auto_compile = auto_compile
88 89 self.preamble = preamble
89 90 self.default_filter = default_filter
90 91
@@ -363,16 +364,17 @@ def _get_module(self, name):
363 364 """Import (or compile and import) named template and return module."""
364 365 names = self._get_filenames(name)
365 366
366   - # compile the template source to .py if it has changed
367   - try:
368   - py_mtime = os.path.getmtime(names['py'])
369   - except OSError:
370   - py_mtime = 0
371   - if os.path.getmtime(names['symplate']) > py_mtime:
372   - self.compile(name)
373   - # .py changed, ensure module is reloaded when imported below
374   - if names['module'] in sys.modules:
375   - sys.modules.pop(names['module'])
  367 + if self.auto_compile:
  368 + # compile the template source to .py if it has changed
  369 + try:
  370 + py_mtime = os.path.getmtime(names['py'])
  371 + except OSError:
  372 + py_mtime = 0
  373 + if os.path.getmtime(names['symplate']) > py_mtime:
  374 + self.compile(name)
  375 + # .py changed, ensure module is reloaded when imported below
  376 + if names['module'] in sys.modules:
  377 + sys.modules.pop(names['module'])
376 378
377 379 # try to import the compiled template; if it doesn't exist (it's never
378 380 # been compiled), compile it and then re-import
@@ -380,6 +382,8 @@ def _get_module(self, name):
380 382 module = __import__(names['module'], globals(), locals(),
381 383 [names['import']])
382 384 except ImportError:
  385 + if not self.auto_compile:
  386 + raise
383 387 self.compile(name)
384 388 module = __import__(names['module'], globals(), locals(),
385 389 [names['import']])
4 tests/test_renderer.py
@@ -36,6 +36,10 @@ def test_check_mtimes_false(self):
36 36 self.assertEquals(self.render('{% template %}cmf2', _renderer=renderer, _increment=0, _adjust_mtime=5), 'cmf2')
37 37 self.assertEquals(self.render('{% template %}cmf3', _renderer=renderer, _increment=0, _adjust_mtime=5), 'cmf2')
38 38
  39 + def test_auto_compile_false(self):
  40 + renderer = utils.Renderer(auto_compile=False)
  41 + self.assertRaises(ImportError, self.render, '{% template %}foo', _renderer=renderer)
  42 +
39 43 def test_modify_path(self):
40 44 saved_path = list(sys.path)
41 45 try:

No commit comments for this range

Something went wrong with that request. Please try again.