Files
Björn Benouarets ee9903b704 init: Initial commit
2026-01-19 14:14:54 +01:00

25 lines
857 B
Python

import os
import json
import re
class Template:
def __init__(self, name: str) -> None:
self.__name = name
self.__path = "templates/" + name + ".json"
self.__template = json.load(open(self.__path))
def __str__(self) -> str:
return json.dumps(self.__template)
def replace_placeholders(self, data: dict) -> dict:
"""Return the template as a dictionary with placeholders replaced."""
template_str = json.dumps(self.__template)
for key, value in data.items():
template_str = template_str.replace(f'{{{{{key}}}}}', str(value))
return json.loads(template_str)
def render(self, data: dict) -> str:
"""Return the template as a JSON string with placeholders replaced."""
return json.dumps(self.replace_placeholders(data))