Mercurial > piecrust2
view piecrust/templating/pystacheengine.py @ 334:b034f6f15e22
bake: Several bug taxonomy-related fixes for incorrect incremental bakes.
* Improve how the baker processes taxonomy terms and figures out what needs
to be re-baked or not.
* Create bake entries for clean taxnomy terms so they're not deleted by an
incremental bake.
* Add more information to bake records.
* Slugify taxonomy terms is now done by the route in one place.
* Fix a bug where the cache key for invalidating rendered segments was not
computed the same way as when the caching was done.
* Fix how term combinations are passed around, rendered, printed, parsed, etc.
(TODO: more word needed in the routing functions)
* Expose to the template whether a taxonomy term is a combination or not.
* Display term combinations better in the built-in theme.
* Rename `route.taxonomy` to `route.taxonomy_name` to prevent confusion.
* Add options to show bake records for previous bakes.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 03 Apr 2015 10:59:50 -0700 |
parents | 139179dc7abd |
children | f4b7c8f183a4 |
line wrap: on
line source
import logging import pystache from piecrust.templating.base import ( TemplateEngine, TemplateNotFoundError, TemplatingError) logger = logging.getLogger(__name__) class PystacheTemplateEngine(TemplateEngine): ENGINE_NAMES = ['mustache'] EXTENSIONS = ['mustache'] def __init__(self): self.renderer = None def renderString(self, txt, data, filename=None): self._ensureLoaded() try: return self.renderer.render(txt, data) except pystache.TemplateNotFoundError as ex: raise TemplateNotFoundError() from ex except pystache.PystacheError as ex: raise TemplatingError(str(ex), filename) from ex def renderFile(self, paths, data): self._ensureLoaded() tpl = None logger.debug("Looking for template: %s" % paths) for p in paths: if not p.endswith('.mustache'): raise TemplatingError( "The Mustache template engine only accepts template " "filenames with a `.mustache` extension. Got: %s" % p) name = p[:-9] # strip `.mustache` try: tpl = self.renderer.load_template(name) except Exception as ex: print(p, ex) pass if tpl is None: raise TemplateNotFoundError() try: return self.renderer.render(tpl, data) except pystache.PystacheError as ex: raise TemplatingError(str(ex)) from ex def _ensureLoaded(self): if self.renderer: return self.renderer = pystache.Renderer( search_dirs=self.app.templates_dirs)