Commit 00c93935 by Pieter van de Bruggen

Refactoring interpolation tags.

parent 1b31b0dc
import re import re
import cgi import cgi
def escapedTag(name):
def func(view):
return cgi.escape(unicode(view.get(name)), True)
return func
def unescapedTag(name):
def func(view):
return unicode(view.get(name))
return func
class Template(object): class Template(object):
tag_re = None tag_re = None
otag, ctag = '{{', '}}' otag, ctag = '{{', '}}'
...@@ -74,20 +84,18 @@ class Template(object): ...@@ -74,20 +84,18 @@ class Template(object):
buffer.append(captures['whitespace']) buffer.append(captures['whitespace'])
captures['whitespace'] = '' captures['whitespace'] = ''
# TODO: Process the remaining tag types. name = captures['name']
fetch = lambda view: view.get(captures['name'])
if captures['tag'] == '!': if captures['tag'] == '!':
pass pass
elif captures['tag'] == '=': elif captures['tag'] == '=':
self.otag, self.ctag = captures['name'].split() self.otag, self.ctag = name.split()
self._compile_regexps() self._compile_regexps()
elif captures['tag'] == '>': elif captures['tag'] == '>':
buffer += self._parse(self.view.partial(captures['name'])) buffer += self._parse(self.view.partial(name))
elif captures['tag'] in ['{', '&']: elif captures['tag'] in ['{', '&']:
buffer.append(lambda view: unicode(fetch(view))) buffer.append(unescapedTag(name))
elif captures['tag'] == '': elif captures['tag'] == '':
buffer.append(lambda view: cgi.escape(unicode(fetch(view)), True)) buffer.append(escapedTag(name))
else: else:
raise Exception("'%s' is an unrecognized type!" % (captures['tag'])) raise Exception("'%s' is an unrecognized type!" % (captures['tag']))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment