Permalink
Please sign in to comment.
Browse files
* first stab at aggressively cached mustache templates. Only variable…
… access supported.. no sections
- Loading branch information...
Showing
with
131 additions
and 201 deletions.
- +1 −7 pystache/__init__.py
- +130 −81 pystache/template.py
- +0 −113 pystache/view.py
| @@ -1,7 +1 @@ | ||
| from pystache.template import Template | ||
| from pystache.view import View | ||
| def render(template, context=None, **kwargs): | ||
| context = context and context.copy() or {} | ||
| context.update(kwargs) | ||
| return Template(template, context).render() | ||
| from template import Template |
| @@ -1,113 +0,0 @@ | ||
| from pystache import Template | ||
| import os.path | ||
| import re | ||
| class View(object): | ||
| # Path where this view's template(s) live | ||
| template_path = '.' | ||
| # Extension for templates | ||
| template_extension = 'mustache' | ||
| # The name of this template. If none is given the View will try | ||
| # to infer it based on the class name. | ||
| template_name = None | ||
| # Absolute path to the template itself. Pystache will try to guess | ||
| # if it's not provided. | ||
| template_file = None | ||
| # Contents of the template. | ||
| template = None | ||
| # Character encoding of the template file. If None, Pystache will not | ||
| # do any decoding of the template. | ||
| template_encoding = None | ||
| def __init__(self, template=None, context=None, **kwargs): | ||
| self.template = template | ||
| self.context = context or {} | ||
| # If the context we're handed is a View, we want to inherit | ||
| # its settings. | ||
| if isinstance(context, View): | ||
| self.inherit_settings(context) | ||
| if kwargs: | ||
| self.context.update(kwargs) | ||
| def inherit_settings(self, view): | ||
| """Given another View, copies its settings.""" | ||
| if view.template_path: | ||
| self.template_path = view.template_path | ||
| if view.template_name: | ||
| self.template_name = view.template_name | ||
| def __contains__(self, needle): | ||
| return hasattr(self, needle) | ||
| def __getitem__(self, attr): | ||
| return getattr(self, attr)() | ||
| def load_template(self): | ||
| if self.template: | ||
| return self.template | ||
| if self.template_file: | ||
| return self._load_template() | ||
| name = self.get_template_name() + '.' + self.template_extension | ||
| if isinstance(self.template_path, basestring): | ||
| self.template_file = os.path.join(self.template_path, name) | ||
| return self._load_template() | ||
| for path in self.template_path: | ||
| self.template_file = os.path.join(path, name) | ||
| if os.path.exists(self.template_file): | ||
| return self._load_template() | ||
| raise IOError('"%s" not found in "%s"' % (name, ':'.join(self.template_path),)) | ||
| def _load_template(self): | ||
| f = open(self.template_file, 'r') | ||
| try: | ||
| template = f.read() | ||
| if self.template_encoding: | ||
| template = unicode(template, self.template_encoding) | ||
| finally: | ||
| f.close() | ||
| return template | ||
| def get_template_name(self, name=None): | ||
| """TemplatePartial => template_partial | ||
| Takes a string but defaults to using the current class' name or | ||
| the `template_name` attribute | ||
| """ | ||
| if self.template_name: | ||
| return self.template_name | ||
| if not name: | ||
| name = self.__class__.__name__ | ||
| def repl(match): | ||
| return '_' + match.group(0).lower() | ||
| return re.sub('[A-Z]', repl, name)[1:] | ||
| def get(self, attr, default): | ||
| attr = self.context.get(attr, getattr(self, attr, default)) | ||
| if hasattr(attr, '__call__'): | ||
| return attr() | ||
| else: | ||
| return attr | ||
| def render(self, encoding=None): | ||
| template = self.load_template() | ||
| return Template(template, self).render(encoding=encoding) | ||
| def __str__(self): | ||
| return self.render() |
0 comments on commit
f704b37