在Flask应用程序中创建第二个Jinja环境

2024-04-26 05:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建第二个Jinja环境来生成LaTeX文档。This snippet使用^{},但我想使用一个自定义加载程序:FileSystemLoader('/path/to/latex/templates')。如何创建一个像snippet一样的env,但是使用我的自定义加载程序?在

LATEX_SUBS = (
    (re.compile(r'\\'), r'\\textbackslash'),
    (re.compile(r'([{}_#%&$])'), r'\\\1'),
    (re.compile(r'~'), r'\~{}'),
    (re.compile(r'\^'), r'\^{}'),
    (re.compile(r'"'), r"''"),
    (re.compile(r'\.\.\.+'), r'\\ldots'),
)

def escape_tex(value):
    newval = value
    for pattern, replacement in LATEX_SUBS:
        newval = pattern.sub(replacement, newval)
    return newval

texenv = app.create_jinja_environment()
texenv.block_start_string = '((*'
texenv.block_end_string = '*))'
texenv.variable_start_string = '((('
texenv.variable_end_string = ')))'
texenv.comment_start_string = '((='
texenv.comment_end_string = '=))'
texenv.filters['escape_tex'] = escape_tex

Tags: 程序restringvaluestartsnippetlatexend
1条回答
网友
1楼 · 发布于 2024-04-26 05:09:04

该代码段使用create_jinja_environment,以便从应用程序模板所在的同一位置加载模板。这很可能是你想要的,只需在你的应用程序的模板文件夹中创建一个目录。在

您仍然可以使用create_jinja_environment,只需替换加载程序,就像代码片段替换env上的其他属性一样。在

texenv = app.create_jinja_environment()
texenv.loader = FileSystemLoader('/path/to/latex/templates')

相关问题 更多 >