🐛 Fix templates after jinja upgrade
Current Jinja versions no longer allow modifying variables set outside
of for loops inside them. So something like:
{% set mark_active = True %}
{% for item in items %}
# do something
{% if mark_active %}
# do something more
{% set mark_active = False %}
{% endif %}
{% endfor %}
no longer works - mark_active will be reset on every iteration of the
loop to its initial value False. The fix is to use namespaces to
hold the modifiable values instead:
{% set ns = namespace(mark_active = True) %}
{% for item in items %}
# do something
{% if ns.mark_active %}
# do something more
{% set ns.mark_active = False %}
{% endif %}
{% endfor %}
See also #1697 and
https://jinja.palletsprojects.com/en/2.11.x/templates/#assignments
The Jinja dependency upgrade thus introduces a breaking change for
plugins as well. However, it was necessary for security reasons: older
versions have a bug that could allow template authors to break out of
the template sandbox. Not a huge risk in the context of OctoPrint
(plugins already have a lot more power than jinja would give them),
but still overdue to fix.
This commit is contained in:
1 parent
2bbe566f72
commit
839dc3e07f
4 files changed
+16
-16
No files matched your search
@@ -8,7 +8,7 @@
|
||||
<div class="tabbable row-fluid">
|
||||
<div class="span3 scrollable" id="about_dialog_menu">
|
||||
<ul class="nav nav-list" id="about_dialog_tabs">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.about.order %}
|
||||
{% set entry, data = templates.about.entries[key] %}
|
||||
{% if data is none %}
|
||||
@@ -17,7 +17,7 @@
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<li id="{{ data._div }}_link"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="{% if mark_active %}active{% set mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="{% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_link" in data %} style="{{ data.styles_link|join(', ') }}" {% elif "styles" in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
<a href="#{{ data._div }}" data-toggle="tab">{{ entry }}</a>
|
||||
@@ -28,14 +28,14 @@
|
||||
</ul>
|
||||
</div>
|
||||
<div class="tab-content span9 scrollable" id="about_dialog_content">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.about.order %}
|
||||
{% set entry, data = templates.about.entries[key] %}
|
||||
{% if data is not none %}
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<div id="{{ data._div }}"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="tab-pane {% if mark_active %}active{% set mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="tab-pane {% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_content" in data %} style="{{ data.styles_content|join(', ') }}" {% elif styles in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
{% try "There was an error with the template {filename} at line number {lineno}: {exception}" %}{% include data.template ignore missing %}{% endtry %}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<div class="tabbable row-fluid">
|
||||
<div class="span3 scrollable" id="settings_dialog_menu">
|
||||
<ul class="nav nav-list" id="settingsTabs">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.settings.order %}
|
||||
{% set entry, data = templates.settings.entries[key] %}
|
||||
{% if data is none %}
|
||||
@@ -17,7 +17,7 @@
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<li id="{{ data._div }}_link"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="{% if mark_active %}active{% set mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="{% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_link" in data %} style="{{ data.styles_link|join(', ') }}" {% elif "styles" in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
<a href="#{{ data._div }}" data-toggle="tab">{{ entry|e }}</a>
|
||||
@@ -28,14 +28,14 @@
|
||||
</ul>
|
||||
</div>
|
||||
<div class="tab-content span9 scrollable" id="settings_dialog_content">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.settings.order %}
|
||||
{% set entry, data = templates.settings.entries[key] %}
|
||||
{% if data is not none %}
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<div id="{{ data._div }}"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="tab-pane {% if mark_active %}active{% set mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="tab-pane {% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_content" in data %} style="{{ data.styles_content|join(', ') }}" {% elif styles in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
{% try "There was an error with the template {filename} at line number {lineno}: {exception}" %}{% include data.template ignore missing %}{% endtry %}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<ul class="nav nav-pills">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.usersettings.order %}
|
||||
{% set entry, data = templates.usersettings.entries[key] %}
|
||||
{% if data is none %}
|
||||
@@ -14,7 +14,7 @@
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<li id="{{ data._div }}_link"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="{% if mark_active %}active{% set mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="{% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_link" in data %} style="{{ data.styles_link|join(', ') }}" {% elif "styles" in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
<a href="#{{ data._div }}" data-toggle="tab">{{ entry|e }}</a>
|
||||
@@ -25,14 +25,14 @@
|
||||
</ul>
|
||||
|
||||
<div class="tab-content">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.usersettings.order %}
|
||||
{% set entry, data = templates.usersettings.entries[key] %}
|
||||
{% if data is not none %}
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<div id="{{ data._div }}"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="tab-pane {% if mark_active %}active{% set mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="tab-pane {% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_content" in data %} style="{{ data.styles_content|join(', ') }}" {% elif styles in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
{% try "There was an error with the template {filename} at line number {lineno}: {exception}" %}{% include data.template ignore missing %}{% endtry %}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<div class="tabbable row-fluid">
|
||||
<div class="span3 scrollable" id="wizard_dialog_menu">
|
||||
<ul class="nav nav-list" id="wizardTabs">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.wizard.order %}
|
||||
{% set entry, data = templates.wizard.entries[key] %}
|
||||
{% if data is none %}
|
||||
@@ -16,7 +16,7 @@
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<li id="{{ data._div }}_link"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="{% if mark_active %}active{% set mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="{% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if "classes_link" in data %}{{ data.classes_link|join(' ') }}{% elif "classes" in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_link" in data %} style="{{ data.styles_link|join(', ') }}" {% elif "styles" in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
<a href="#{{ data._div }}" data-toggle="tab">{% if "mandatory" in data and data.mandatory %}<strong>{{ entry }}</strong>{% else %}{{ entry }}{% endif %}</a>
|
||||
@@ -27,14 +27,14 @@
|
||||
</ul>
|
||||
</div>
|
||||
<div class="tab-content span9 scrollable" id="wizard_dialog_content">
|
||||
{% set mark_active = True %}
|
||||
{% set ns = namespace(mark_active = True) %}
|
||||
{% for key in templates.wizard.order %}
|
||||
{% set entry, data = templates.wizard.entries[key] %}
|
||||
{% if data is not none %}
|
||||
{% if "custom_bindings" not in data or data["custom_bindings"] %}<!-- ko allowBindings: false -->{% endif %}
|
||||
<div id="{{ data._div }}"
|
||||
{% if "data_bind" in data %}data-bind="{{ data.data_bind }}"{% endif %}
|
||||
class="tab-pane {% if mark_active %}active{% set mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
class="tab-pane {% if ns.mark_active %}active{% set ns.mark_active = False %}{% endif %} {% if classes_content in data %}{{ data.classes_content|join(' ') }}{% elif classes in data %}{{ data.classes|join(' ') }}{% endif %}"
|
||||
{% if "styles_content" in data %} style="{{ data.styles_content|join(', ') }}" {% elif styles in data %} style="{{ data.styles|join(', ') }}" {% endif %}
|
||||
>
|
||||
{% if "mandatory" in data and data.mandatory %}
|
||||
|
||||
Reference in new issue
Block a user