aquifer/networking/templates.py

42 lines
1.1 KiB
Python

def render(template, **kwargs):
[startString, endString] = ['{{', '}}']
[startLength, endLength] = [len(startString), len(endString)]
with open(template) as f:
data = f.read()
tokenCaret = 0
result = ''
isRendering = True
while isRendering:
start = data.find(startString, tokenCaret)
end = data.find(endString, start)
isRendering = start != -1 and end != -1
if isRendering:
token = data[start + startLength:end].strip()
result = (
result +
data[tokenCaret:start] +
replaceToken(token, kwargs)
)
tokenCaret = end + endLength
else:
result = result + data[tokenCaret:]
return result
def replaceToken(token, values):
result = str(values[token]) if token in values else ''
result = result.replace('&', '&')
result = result.replace('"', '"')
result = result.replace("'", ''')
result = result.replace('>', '>')
result = result.replace('<', '&lt;')
return result