Answer the question
In order to leave comments, you need to log in
How to write a simple Jinja2 extension?
I'm trying to write a simple extension at the level of recoding all content to base64, but it doesn't work. I don't understand where is the error:
class Base64Extension(Extension):
tags = {'base64'}
def __init__(self, environment):
super(Base64Extension, self).__init__(environment)
def parse(self, parser):
lineno = next(parser.stream).lineno
args = [parser.parse_expression()]
call = self.call_method('_render', args)
return nodes.Output([call], lineno=lineno)
def _render(self, text):
return base64.b64encode(bytes(text, 'utf-8')).decode('utf-8')
env = Environment(loader=FileSystemLoader('project/test'), extensions=[Base64Extension], trim_blocks=True,block_start_string='',variable_start_string='[', variable_end_string=']')
print(env.get_template('data2.txt').render())
[base64]
Привет текста.
[endbase64]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question