Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
BoQsc committed Jul 31, 2024
1 parent 734b9f7 commit 067cfc9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions webserver/Webserver/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Welcome, {{ username }}!</h1>
<p>This is a {{ page_type }} page.</p>
</body>
</html>
28 changes: 28 additions & 0 deletions webserver/Webserver/templatetest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import re
import inspect

def render_template(template_file, **kwargs):
# Read the template from file
with open(template_file, 'r') as file:
template_string = file.read()

# Get the calling frame
frame = inspect.currentframe().f_back

# Combine local variables from the calling scope with passed kwargs
variables = {**frame.f_locals, **kwargs}

def replace_var(match):
var_name = match.group(1)
return str(variables.get(var_name, f'{{{{ {var_name} }}}}'))

rendered_content = re.sub(r'\{\{ (\w+) \}\}', replace_var, template_string)
return rendered_content

# Example usage:
username = "waffles"
title = "My Awesome Page"
page_type = "sample"

rendered_html = render_template('template.html')
print(rendered_html)

0 comments on commit 067cfc9

Please sign in to comment.