How to set a value of a variable inside a template code?

Alexis picture Alexis · Jul 1, 2009 · Viewed 313.5k times · Source

Say I have a template

<html>
<div>Hello {{name}}!</div>
</html>

While testing it, it would be useful to define the value of the variable without touching the python code that invokes this template. So I'm looking for something like this

{% set name="World" %}     
<html>
<div>Hello {{name}}!</div>
</html>

Does something like this exists in Django?

Answer

John picture John · Jul 1, 2009

You can use the with template tag.

{% with name="World" %}     
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}