title: Accessing external variables from within a Jinja2 macro
body: <p>I have the following Jinja2 files but the macro <code>do_stuff</code> doesn't appear to be able to access the values of <code>something</code> and <code>something_else</code> that are defined in <code>my_template.jinja2</code>.</p>
  
  <p>Is there a way to make this possible? I'm aware I can pass them in manually to the macro, but as their values stay the same for every invocation of the macro in a template file, it would be nice not to have to do that.</p>
  
  <p>If I embed the macro directly in <code>my_template.jinja2</code> it works but then it would have to be duplicated across all templates that use it.</p>
  
  <p><strong>macro.jinja2</strong></p>
  
  <pre><code>{% macro do_stuff(value) %}
       &lt;p&gt;Something is {{ something | default('nothing') }}.&lt;/p&gt;
       &lt;p&gt;Something else is {{ something_else | default ('nothing') }}.&lt;/p&gt;
       &lt;p&gt;Values is {{ value }}.&lt;/p&gt;
  {% endmacro %}
  </code></pre>
  
  <p><strong>base_template.jinja2</strong></p>
  
  <pre><code>{% from 'macros.jinja2' import do_stuff %}
  {# Other common stuff goes here #}
  </code></pre>
  
  <p><strong>my_template.jinja2</strong></p>
  
  <pre><code>{% extends 'base_template.jinja2' %}
  
  {% set something = "foo" %}
  {% set something_else = "bar" %}
  
  {# Content #}
  {{ do_stuff("baz1") }}
  {# More content #}
  {{ do_stuff("baz2") }}
  {# More content #}
  {# etc. #}
  </code></pre>