Coverage for /Users/davegaeddert/Development/dropseed/plain/plain/plain/templates/jinja/extensions.py: 68%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-16 22:04 -0500

1from jinja2 import nodes 

2from jinja2.ext import Extension 

3 

4 

5class InclusionTagExtension(Extension): 

6 """Intended to be subclassed""" 

7 

8 # tags = {'inclusion_tag'} 

9 tags: set[str] 

10 template_name: str 

11 

12 def parse(self, parser): 

13 lineno = next(parser.stream).lineno 

14 args = [ 

15 nodes.DerivedContextReference(), 

16 ] 

17 kwargs = [] 

18 while parser.stream.current.type != "block_end": 

19 if parser.stream.current.type == "name": 

20 key = parser.stream.current.value 

21 parser.stream.skip() 

22 parser.stream.expect("assign") 

23 value = parser.parse_expression() 

24 kwargs.append(nodes.Keyword(key, value)) 

25 else: 

26 args.append(parser.parse_expression()) 

27 

28 call = self.call_method("_render", args=args, kwargs=kwargs, lineno=lineno) 

29 return nodes.CallBlock(call, [], [], []).set_lineno(lineno) 

30 

31 def _render(self, context, *args, **kwargs): 

32 context = self.get_context(context, *args, **kwargs) 

33 template = self.environment.get_template(self.template_name) 

34 return template.render(context) 

35 

36 def get_context(self, context, *args, **kwargs): 

37 raise NotImplementedError( 

38 "You need to implement the `get_context` method in your subclass." 

39 )