-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
92 lines (81 loc) · 2.23 KB
/
example.py
File metadata and controls
92 lines (81 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Example usage of the Custom Template Engine
"""
from template_engine import CustomTemplateEngine
def main():
# Create engine instance
engine = CustomTemplateEngine()
# Sample data - list of dictionaries
data = {
'page_title': 'Employee Directory',
'department': 'Engineering',
'employees': [
{
'name': 'John Doe',
'position': 'Senior Developer',
'salary': 85000,
'skills': ['Python', 'JavaScript', 'SQL']
},
{
'name': 'Jane Smith',
'position': 'DevOps Engineer',
'salary': 90000,
'skills': ['Docker', 'Kubernetes', 'AWS']
},
{
'name': 'Mike Johnson',
'position': 'Frontend Developer',
'salary': 75000,
'skills': ['React', 'Vue.js', 'CSS']
}
]
}
# HTML template with loops using string.Template syntax
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>$page_title</title>
<style>
.employee { border: 1px solid #ccc; margin: 10px; padding: 10px; }
.name { font-weight: bold; color: #333; }
.position { color: #666; }
</style>
</head>
<body>
<h1>$page_title</h1>
<h2>Department: $department</h2>
<div class="employees">
{% for emp in employees %}
<div class="employee">
<div class="name">$emp.name</div>
<div class="position">$emp.position</div>
<div>Salary: $emp.salary</div>
</div>
{% endfor %}
</div>
</body>
</html>
"""
# Text template example using string.Template syntax
text_template = """
$page_title
==========================================
Department: $department
Employee List:
{% for emp in employees %}
- $emp.name ($emp.position)
Salary: $emp.salary
{% endfor %}
"""
# Render templates
print("HTML Output:")
print("=" * 50)
html_result = engine.render(html_template, data)
print(html_result)
print("\n\nText Output:")
print("=" * 50)
text_result = engine.render(text_template, data)
print(text_result)
if __name__ == "__main__":
main()