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