The main difference between string.Template from Python's string module and jinja2.Template from the Jinja2 templating engine lies in their syntax and features.
Step 1: Creating the Template
string.Template:
from string import Template
temp = Template("Today is $today and tomorrow is $tomorrow.")
- Syntax: In
string.Template, placeholders are denoted by a dollar sign ($), followed by the variable name (e.g.,$today,$tomorrow).
jinja2.Template:
from jinja2 import Template
temp = Template("Today is {{today}} and tomorrow is {{tomorrow}}.")
- Syntax: In Jinja2, placeholders are denoted by double curly braces (
{{}}), surrounding the variable name (e.g.,{{today}},{{tomorrow}}).
Step 2: Rendering the Template
string.Template:
out = temp.substitute(today="Monday")
print(out)
- Rendering Method: The
substitute()method replaces the placeholders with the provided values. - Error Handling: If a value for a placeholder is not provided, it raises a
KeyError.
If you want to avoid an error when a variable is missing, you can use safe_substitute():
out = temp.safe_substitute(today="Monday")
print(out)
safe_substitute(): This method substitutes provided values and leaves placeholders for missing ones, instead of raising an error.
jinja2.Template:
out = temp.render(today="Monday")
print(out)
- Rendering Method: Jinja2 uses the
render()method to replace placeholders with provided values. - Error Handling: If a variable is missing in Jinja2, it does not throw an error. Instead, it leaves the placeholder empty (i.e., it renders nothing for that variable).
Summary of Differences:
-
Syntax:
string.Template:$variablejinja2.Template:{{variable}}
-
Rendering:
string.Template: Usessubstitute()orsafe_substitute().jinja2.Template: Usesrender().
-
Error Handling:
string.Template:substitute(): Throws aKeyErrorif any placeholder is missing.safe_substitute(): Leaves missing variables unchanged, without raising an error.
jinja2.Template: Leaves missing variables empty (blank) without throwing an error.