1
1
from django .template .loader_tags import BlockNode , ExtendsNode
2
- from django .template import loader , Context , RequestContext
3
- from django .http import HttpResponse
2
+ from django .template import loader , Context
4
3
5
4
6
5
7
- class BlockNotFound (Exception ):
8
- pass
9
-
10
-
11
6
def get_template (template ):
12
7
if isinstance (template , (tuple , list )):
13
8
return loader .select_template (template )
14
9
return loader .get_template (template )
15
10
16
11
def render_template_blocks (template , block_list , context ):
17
12
"""
18
- Renders a single block from a template. This template should have previously been rendered.
13
+ Renders a list of blocks from a template.
14
+ Return a dictionary of rendered template blocks.
19
15
"""
20
- return render_template_blocks_nodelist (template .nodelist , block_list , context )
16
+ return render_blocks (template .nodelist , block_list , context )
21
17
22
- def render_template_blocks_nodelist (nodelist , block_list , context ):
18
+ def render_blocks (nodelist , block_list , context ):
23
19
block_map = {}
24
-
20
+
25
21
for node in nodelist :
26
22
if isinstance (node , BlockNode ) and node .name in block_list :
27
23
block_map .setdefault (node .name , node .render (Context (context )))
28
24
29
25
for key in ('nodelist' , 'nodelist_true' , 'nodelist_false' ):
30
26
if hasattr (node , key ):
31
- try :
32
- inner_block_map = render_template_blocks_nodelist (getattr (node , key ), block_list , context )
33
- except :
34
- pass
35
- else :
36
- block_map .update (inner_block_map )
37
- inner_block_map = {}
27
+ inner_block_map = render_blocks (getattr (node , key ), block_list , context )
28
+ block_map .update (inner_block_map )
38
29
39
30
for node in nodelist :
40
31
if isinstance (node , ExtendsNode ):
41
- try :
42
- inner_block_map = render_template_blocks (node .get_parent (context ), block_list , context )
43
- except BlockNotFound :
44
- pass
45
- else :
46
- block_map .update (inner_block_map )
32
+ inner_block_map = render_template_blocks (node .get_parent (context ), block_list , context )
33
+ block_map .update (inner_block_map )
47
34
48
- return block_map
49
-
50
- def render_block_to_string (template_name , block_list , dictionary = {}, context_instance = None ):
51
- """
52
- Loads the given template_name and renders the given block with the given dictionary as
53
- context. Returns a string.
54
- """
55
- template = get_template (template_name )
56
-
57
- if context_instance is not None :
58
- context_instance .update (dictionary )
59
- else :
60
- context_instance = Context (dictionary )
61
-
62
- template .render (context_instance )
63
-
64
- return render_template_blocks (template , block_list , context_instance )
65
-
66
- def direct_block_to_template (request , template , block_list , extra_context = None , mimetype = None , ** kwargs ):
67
- """
68
- Render a given block in a given template with any extra URL parameters in the context as
69
- ``{{ params }}``.
70
- """
71
- if extra_context is None :
72
- extra_context = {}
73
- dictionary = {'params' : kwargs }
74
- for key , value in extra_context .items ():
75
- if callable (value ):
76
- dictionary [key ] = value ()
77
- else :
78
- dictionary [key ] = value
79
- c = RequestContext (request , dictionary )
80
- t = get_template (template )
81
- t .render (c )
82
-
83
- return HttpResponse (render_template_blocks (t , block_list , c ), mimetype = mimetype )
35
+ return block_map
0 commit comments