Skip to content

Commit 46d2b4d

Browse files
committed
fix: fix ifinstalled tag for Django 3+
Fixes #1972
1 parent 3701d79 commit 46d2b4d

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

mezzanine/core/templatetags/mezzanine_tags.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ def is_installed(app_name):
155155
return app_name in settings.INSTALLED_APPS
156156

157157

158+
class OptionalNodeList(Node):
159+
def __init__(self, nodelist=None):
160+
self.nodelist = nodelist
161+
162+
def render(self, context):
163+
if self.nodelist is None:
164+
return ""
165+
return self.nodelist.render(context)
166+
167+
158168
@register.tag
159169
def ifinstalled(parser, token):
160170
"""
@@ -165,9 +175,8 @@ def ifinstalled(parser, token):
165175
{% include "app_name/template.html" %}
166176
{% endifinstalled %}
167177
168-
so we need to manually pull out all tokens if the app isn't
169-
installed, since if we used a normal ``if`` tag with a False arg,
170-
the include tag will still try and find the template to include.
178+
If the app is not installed the entire contents of the block will be skipped without
179+
being parsed (like a comment).
171180
"""
172181
try:
173182
tag, app = token.split_contents()
@@ -179,25 +188,13 @@ def ifinstalled(parser, token):
179188
)
180189

181190
end_tag = "end" + tag
182-
unmatched_end_tag = 1
183-
if app.strip("\"'") not in settings.INSTALLED_APPS:
184-
while unmatched_end_tag:
185-
token = parser.tokens.pop(0)
186-
if token.token_type == TokenType.BLOCK:
187-
block_name = token.contents.split()[0]
188-
if block_name == tag:
189-
unmatched_end_tag += 1
190-
if block_name == end_tag:
191-
unmatched_end_tag -= 1
192-
parser.tokens.insert(0, token)
193-
nodelist = parser.parse((end_tag,))
194-
parser.delete_first_token()
195-
196-
class IfInstalledNode(Node):
197-
def render(self, context):
198-
return nodelist.render(context)
199-
200-
return IfInstalledNode()
191+
if app.strip("\"'") in settings.INSTALLED_APPS:
192+
nodelist = parser.parse((end_tag,))
193+
parser.delete_first_token()
194+
else:
195+
nodelist = None
196+
parser.skip_past(end_tag)
197+
return OptionalNodeList(nodelist)
201198

202199

203200
@register.render_tag

0 commit comments

Comments
 (0)