Expected Behavior
When a template includes a macro and uses it the template globals should be accessible to templates included by the macro.
Actual Behavior
When including a macro (that includes another template) the nested template does not have access to the template .globals. If you force the variable into the template.globals after calling get_template then it works.
This does not seem to be an issue when the macro is defined and used in the main template, only when the macro is included into the template.
Example Code
from jinja2 import DictLoader, Environment
loader = DictLoader({
'test': """
{% import 'macro' as macros %}
{{ macros.testing() }}""",
'macro': """
{% macro testing() %}
macro reference to global: {{foo}}
{% include 'nested' %}
{% endmacro %}""",
'nested': 'nested reference to global: {{foo}}'
})
env = Environment(loader=loader, cache_size=0)
# When the template is provided globals and includes a macro
# the global variable is not available to the macro.
template = env.get_template('test', globals={'foo': 'bar'})
# This fails and doesn't show the global foo value:
# macro reference to global:
# nested reference to global:
print template.render()
# If you set the global value after getting the template then the macro
# is able to reference the global variable.
template = env.get_template('test')
template.globals['foo'] = 'bar'
# This works, but I think it is a hack or isn't recommended.
# macro reference to global: bar
# nested reference to global: bar
print template.render()
Your Environment
- Python version: 2.7.10
- Jinja version: 2.9.5
Expected Behavior
When a template includes a macro and uses it the template globals should be accessible to templates included by the macro.
Actual Behavior
When including a macro (that includes another template) the nested template does not have access to the template
.globals. If you force the variable into thetemplate.globalsafter callingget_templatethen it works.This does not seem to be an issue when the macro is defined and used in the main template, only when the macro is included into the template.
Example Code
Your Environment