1
|
|
|
# vim: set fileencoding=utf-8 : |
2
|
|
|
""" |
3
|
|
|
django if templatetag patch |
4
|
|
|
""" |
5
|
|
|
import django |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
if django.VERSION < (1, 4): |
9
|
|
|
from django.template import Node |
10
|
|
|
from django.template import NodeList |
11
|
|
|
from django.template import VariableDoesNotExist |
12
|
|
|
from django.template import TemplateSyntaxError |
13
|
|
|
try: |
14
|
|
|
from django.template.base import TextNode |
15
|
|
|
except ImportError: |
16
|
|
|
from django.template import TextNode |
17
|
|
|
# copied from django 1.4b |
18
|
|
|
class IfNode(Node): |
19
|
|
|
def __init__(self, conditions_nodelists): |
20
|
|
|
self.conditions_nodelists = conditions_nodelists |
21
|
|
|
def __repr__(self): |
22
|
|
|
return "<IfNode>" |
23
|
|
|
def __iter__(self): |
24
|
|
|
for _, nodelist in self.conditions_nodelists: |
25
|
|
|
for node in nodelist: |
26
|
|
|
yield node |
27
|
|
|
@property |
28
|
|
|
def nodelist(self): |
29
|
|
|
return NodeList(node for _, nodelist in self.conditions_nodelists for node in nodelist) |
30
|
|
|
def render(self, context): |
31
|
|
|
for condition, nodelist in self.conditions_nodelists: |
32
|
|
|
if condition is not None: # if / elif clause |
33
|
|
|
try: |
34
|
|
|
match = condition.eval(context) |
35
|
|
|
except VariableDoesNotExist: |
36
|
|
|
match = None |
37
|
|
|
else: # else clause |
38
|
|
|
match = True |
39
|
|
|
if match: |
40
|
|
|
return nodelist.render(context) |
41
|
|
|
return '' |
42
|
|
|
# copied from django 1.4b |
43
|
|
|
def parse(self, parse_until=None): |
44
|
|
|
if parse_until is None: |
45
|
|
|
parse_until = [] |
46
|
|
|
nodelist = self.create_nodelist() |
47
|
|
|
while self.tokens: |
48
|
|
|
token = self.next_token() |
49
|
|
|
# Use the raw values here for TOKEN_* for a tiny performance boost. |
50
|
|
|
if token.token_type == 0: # TOKEN_TEXT |
51
|
|
|
self.extend_nodelist(nodelist, TextNode(token.contents), token) |
52
|
|
|
elif token.token_type == 1: # TOKEN_VAR |
53
|
|
|
if not token.contents: |
54
|
|
|
self.empty_variable(token) |
55
|
|
|
filter_expression = self.compile_filter(token.contents) |
56
|
|
|
var_node = self.create_variable_node(filter_expression) |
57
|
|
|
self.extend_nodelist(nodelist, var_node, token) |
58
|
|
|
elif token.token_type == 2: # TOKEN_BLOCK |
59
|
|
|
try: |
60
|
|
|
command = token.contents.split()[0] |
61
|
|
|
except IndexError: |
62
|
|
|
self.empty_block_tag(token) |
63
|
|
|
if command in parse_until: |
64
|
|
|
# put token back on token list so calling |
65
|
|
|
# code knows why it terminated |
66
|
|
|
self.prepend_token(token) |
67
|
|
|
return nodelist |
68
|
|
|
# execute callback function for this tag and append |
69
|
|
|
# resulting node |
70
|
|
|
self.enter_command(command, token) |
71
|
|
|
try: |
72
|
|
|
compile_func = self.tags[command] |
73
|
|
|
except KeyError: |
74
|
|
|
self.invalid_block_tag(token, command, parse_until) |
75
|
|
|
try: |
76
|
|
|
compiled_result = compile_func(self, token) |
77
|
|
|
except TemplateSyntaxError as e: |
78
|
|
|
if not self.compile_function_error(token, e): |
79
|
|
|
raise |
80
|
|
|
self.extend_nodelist(nodelist, compiled_result, token) |
81
|
|
|
self.exit_command() |
82
|
|
|
if parse_until: |
83
|
|
|
self.unclosed_block_tag(parse_until) |
84
|
|
|
return nodelist |
85
|
|
|
def parser_patch(instance): |
86
|
|
|
instance.__class__.parse = parse |
87
|
|
|
return instance |
88
|
|
|
else: |
89
|
|
|
from django.template.defaulttags import IfNode |
90
|
|
|
parser_patch = lambda instance: instance |
91
|
|
|
|