Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions pystache/template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
import cgi
import collections
import os
import copy
import types

try:
import markupsafe
Expand All @@ -17,7 +17,7 @@
class Modifiers(dict):
"""Dictionary with a decorator for assigning functions to keys."""

def set(self, key):
def set(self, key, func=None):
"""
Decorator function to set the given key to the decorated function.

Expand All @@ -32,7 +32,13 @@ def set(self, key):
def setter(func):
self[key] = func
return func
return setter

if isinstance(func, (types.FunctionType, types.MethodType)):
# called directly, before the method has been bound
return setter(func)
else:
# called as a decorator
return setter


class Template(object):
Expand Down Expand Up @@ -105,7 +111,7 @@ def _render_sections(self, template, view):
template = literal(template.replace(section, replacer))

return template

def _render_tags(self, template):
while True:
match = self.tag_re.search(template)
Expand Down Expand Up @@ -134,7 +140,6 @@ def _render_list(self, template, listing):

return ''.join(insides)

@modifiers.set(None)
def _render_tag(self, tag_name):
raw = self.view.get(tag_name, '')

Expand All @@ -146,30 +151,31 @@ def _render_tag(self, tag_name):
return ''

return escape(raw)
_render_tag = modifiers.set(None, _render_tag)

@modifiers.set('!')
def _render_comment(self, tag_name):
return ''
_render_comment = modifiers.set('!', _render_comment)

@modifiers.set('>')
def _render_partial(self, template_name):
from pystache import Loader
markup = Loader().load_template(template_name, self.view.template_path, encoding=self.view.template_encoding)
template = Template(markup, self.view)
return template.render()
_render_partial = modifiers.set('>', _render_partial)

@modifiers.set('=')
def _change_delimiter(self, tag_name):
"""Changes the Mustache delimiter."""
self.otag, self.ctag = tag_name.split(' ')
self._compile_regexps()
return ''
_change_delimiter = modifiers.set('=', _change_delimiter)

@modifiers.set('{')
@modifiers.set('&')
def render_unescaped(self, tag_name):
"""Render a tag without escaping it."""
return literal(self.view.get(tag_name, ''))
render_unescaped = modifiers.set('{', render_unescaped)
render_unescaped = modifiers.set('&', render_unescaped)

def render(self, encoding=None):
template = self._render_sections(self.template, self.view)
Expand Down
2 changes: 1 addition & 1 deletion pystache/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class View(object):
def __init__(self, template=None, context=None, **kwargs):
self.template = template
context = context or {}
context.update(**kwargs)
context.update(kwargs)

self.context_list = [context]

Expand Down
6 changes: 5 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def test_using_list_of_paths(self):
def test_non_existent_template_fails(self):
loader = pystache.Loader()

self.assertRaises(IOError, loader.load_template, 'simple', 'doesnt_exist')
self.assertRaises(IOError, loader.load_template, 'simple', 'doesnt_exist')


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ def test_template_partial_extension(self):

Again, Welcome!
""")


if __name__ == '__main__':
unittest.main()