Skip to content

Commit 908f518

Browse files
authored
Merge pull request #2 from gone/jinja-extends-include
Jinja extends include
2 parents 555ad19 + 59bb7eb commit 908f518

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pattern_library/loader_tags.py

+47
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,50 @@ def do_include(parser, token):
150150
extra_context=namemap,
151151
isolated_context=isolated_context,
152152
)
153+
154+
def visit_extends(self, node, frame):
155+
"""This method overrides the jinja extends tag
156+
Is called as part of the compiler CodeGenerator
157+
and adds a line to use the template_new_context as
158+
part of the runtime render to pull in the dpl context
159+
Handles visiting extends
160+
"""
161+
from .monkey_utils import jinja_visit_Extends
162+
163+
jinja_visit_Extends(self, node, frame)
164+
# addition to update the context with dpl context
165+
# calls the template_new_context method below when
166+
# invoked at runtime
167+
self.writeline(
168+
"parent_template.new_context(context.get_all(), True,"
169+
f" {self.dump_local_context(frame)})"
170+
)
171+
172+
173+
def template_new_context(
174+
self,
175+
vars=None,
176+
shared=False,
177+
locals=None,
178+
):
179+
"""This method overrides the jinja include tag
180+
Is called as part of Template.render by jinja2 and is updated
181+
to pull in the dpl context
182+
Create a new :class:`Context` for this template. The vars
183+
provided will be passed to the template. Per default the globals
184+
are added to the context. If shared is set to `True` the data
185+
is passed as is to the context without adding the globals.
186+
187+
`locals` can be a dict of local variables for internal usage.
188+
"""
189+
from jinja2.runtime import new_context
190+
191+
if is_pattern_library_context(vars or {}) and (
192+
pattern_context := get_pattern_context(self.name)
193+
):
194+
for k, v in pattern_context.items():
195+
vars.setdefault(k, v)
196+
197+
return new_context(
198+
self.environment, self.name, self.blocks, vars, shared, self.globals, locals
199+
)

pattern_library/monkey_utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,24 @@ def node_render(context):
9696
return original_node
9797

9898
return tag_func
99+
100+
# have to export the original jinja visit Extends
101+
# in the case jinja tags are being overriden
102+
jinja_visit_Extends = None
103+
104+
def override_jinja_tags():
105+
"""
106+
Overrides jinja extends and include tags for use in your pattern library.
107+
Call it in your settings to override tags
108+
"""
109+
global jinja_visit_Extends
110+
try:
111+
from jinja2.compiler import CodeGenerator as JinjaCodeGenerator
112+
from jinja2.environment import Template as JinjaTemplate
113+
except ModuleNotFoundError:
114+
ModuleNotFoundError("install jinja2 to override jinja tags")
115+
116+
from .loader_tags import template_new_context, visit_extends
117+
jinja_visit_Extends = JinjaCodeGenerator.visit_Extends
118+
JinjaTemplate.new_context = template_new_context
119+
JinjaCodeGenerator.visit_Extends = visit_extends

0 commit comments

Comments
 (0)