Skip to content

Commit 80348d4

Browse files
committed
add: context element to adjust context
1 parent b222574 commit 80348d4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

htmlgenerator/base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ def render(self, context: dict):
214214
yield from self.render_children(context)
215215

216216

217+
class Context(BaseElement):
218+
"""
219+
Pass additional variables into the context.
220+
The additional context names are namespaced to the current element and its child elements.
221+
It can be helpfull for shadowing or aliasing a name in the context.
222+
This element is required because context is otherwise only set by the render function and the loop-variable of Iierator
223+
"""
224+
225+
additional_context: dict = {}
226+
227+
def __init__(self, *children, **kwargs):
228+
self.additional_context = kwargs
229+
super().__init__(*children)
230+
231+
def render(self, context):
232+
return super().render({**context, **self.additional_context})
233+
234+
217235
def treewalk(
218236
element: typing.List,
219237
ancestors: typing.Tuple[BaseElement, ...],

0 commit comments

Comments
 (0)