@@ -155,6 +155,16 @@ def is_installed(app_name):
155
155
return app_name in settings .INSTALLED_APPS
156
156
157
157
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
+
158
168
@register .tag
159
169
def ifinstalled (parser , token ):
160
170
"""
@@ -165,9 +175,8 @@ def ifinstalled(parser, token):
165
175
{% include "app_name/template.html" %}
166
176
{% endifinstalled %}
167
177
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).
171
180
"""
172
181
try :
173
182
tag , app = token .split_contents ()
@@ -179,25 +188,13 @@ def ifinstalled(parser, token):
179
188
)
180
189
181
190
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 )
201
198
202
199
203
200
@register .render_tag
0 commit comments