2

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..

3
  • Why can't you use your first solution multiple times? Commented Nov 21, 2020 at 18:53
  • @JeffUK .. You mean in this way """""" Template(html123).safe_substitute(Var1=LockingVar) ..safe_substitute(Var1=LockingVar) """"" .. i tried but not working for multiple Commented Nov 21, 2020 at 18:54
  • would jinja be an option for you? Commented Nov 21, 2020 at 18:57

1 Answer 1

2

Provide a dict to safe_substitute

>>> params = {
...  'Var1':'aa',
...  'Var2':'bb',
...  'Var3':'cc'
... }

>>> Template("$Var1 $Var2 $Var3").safe_substitute(params)  
"aa bb cc"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.