Reputation: 863
we have below html code which calls in python with one varibale
html123='''
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<div>This text is $Var1</div>
</body>
</html>
</html> '''
for one variable i tried below approach
from string import Template
s = Template(html123).safe_substitute(Var1=LockingVar)
If i have multiple variables
html123='''
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<div>This text is $Var1</div>
<div>This text is $Var2</div>
<div>This text is $Var3</div>
</body>
</html>
</html> '''
but if we have multiple variables Var1,var2,var3 inside html tag , how can we map those variables values .
Please any suggestion..
Upvotes: 2
Views: 304
Reputation: 6298
Provide a dict to safe_substitute
>>> params = {
... 'Var1':'aa',
... 'Var2':'bb',
... 'Var3':'cc'
... }
>>> Template("$Var1 $Var2 $Var3").safe_substitute(params)
"aa bb cc"
Upvotes: 2