marshaller/net/templates.py

42 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-10-23 15:43:36 -04:00
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] +
replace_token(token, kwargs)
2023-10-23 15:43:36 -04:00
)
tokenCaret = end + endLength
else:
result = result + data[tokenCaret:]
return result
def replace_token(token, values):
2023-10-23 15:43:36 -04:00
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