title: Is there a way to pass a variable to an 'extended' template in Django?
body: <p>I want to add some flexibility to my layout template, but I can't find any way to do so. </p>
  
  <p>I'm looking for a way to <code>extend</code> my layout template with variable, i.e. to pass a variable up in the template tree, not down.</p>
  
  <pre><code># views.py
  def my_view_func(request):
      return render(request, "child.html")
  </code></pre>
  
  <hr>
  
  <pre><code># child.html
  {% extends 'layout.html' with show_sidebar=True sidebar_width_class="width_4" %}
  
  &lt;div&gt;Templates stuff here&lt;/div&gt;
  </code></pre>
  
  <hr>
  
  <pre><code># layout.html
  {% if show_sidebar %}
      &lt;div class="{{ sidebar_width_class }}"&gt;
          {% block sidebar %}{% endblock %}
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>I have to maintain four templates with a difference in a few lines of code. For example, I have two templates that differ from each other by a sidebar width class. Am I doing something wrong?</p>