diff options
author | Chris Wanstrath <chris@ozmm.org> | 2009-11-12 20:52:54 -0800 |
---|---|---|
committer | Chris Wanstrath <chris@ozmm.org> | 2009-11-12 20:52:54 -0800 |
commit | 40b904a365450910228959a0fa307007bd744bae (patch) | |
tree | 39a40a5acac8d3a2bb48d14227fd0419a6007b59 | |
parent | d02b657f0f9d2a659287279d3b26863018fb0659 (diff) | |
download | pystache-40b904a365450910228959a0fa307007bd744bae.tar.gz |
implement partials, make test pass
-rw-r--r-- | pystache/template.py | 15 | ||||
-rw-r--r-- | pystache/view.py | 37 | ||||
-rw-r--r-- | tests/test_examples.py | 3 |
3 files changed, 48 insertions, 7 deletions
diff --git a/pystache/template.py b/pystache/template.py index 7bde535..6e6901c 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -8,7 +8,8 @@ class Template(object): tag_types = { None: 'tag', '!': 'comment', - '{': 'unescaped' + '{': 'unescaped', + '>': 'partial' } def __init__(self, template, context=None): @@ -63,7 +64,7 @@ class Template(object): return template def render_tag(self, tag_name, context): - """Given a tag name and context, finds and renders the tag.""" + """Given a tag name and context, finds, escapes, and renders the tag.""" return cgi.escape(context.get(tag_name, '')) def render_comment(self, tag_name=None, context=None): @@ -73,3 +74,13 @@ class Template(object): def render_unescaped(self, tag_name=None, context=None): """Render a tag without escaping it.""" return context.get(tag_name, '') + + def render_partial(self, tag_name=None, context=None): + """Renders a partial within the current context.""" + # Import view here to avoid import loop + from pystache.view import View + + view = View(context=context) + view.template_name = tag_name + + return view.render() diff --git a/pystache/view.py b/pystache/view.py index 02bde7c..b31aac2 100644 --- a/pystache/view.py +++ b/pystache/view.py @@ -9,6 +9,10 @@ class View(object): # 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 @@ -19,14 +23,35 @@ class View(object): def __init__(self, template=None, context=None, **kwargs): self.template = template self.context = context or {} - self.context.update(kwargs) + + # 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 not self.template_file: - name = self.template_name() + '.' + self.template_extension + name = self.get_template_name() + '.' + self.template_extension self.template_file = os.path.join(self.template_path, name) f = open(self.template_file, 'r') @@ -34,10 +59,14 @@ class View(object): f.close() return template - def template_name(self, name=None): + def get_template_name(self, name=None): """TemplatePartial => template_partial - Takes a string but defaults to using the current class' name. + 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__ diff --git a/tests/test_examples.py b/tests/test_examples.py index 2246129..76da7ef 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -24,4 +24,5 @@ class TestView(unittest.TestCase): self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>") def test_template_partial(self): - self.assertEquals(TemplatePartial().render(), 'blah') + self.assertEquals(TemplatePartial().render(), """<h1>Welcome</h1> +Again, Welcome!""") |