我希望在我的烧瓶博客的文章中提供宏。当我的博客正文发生更改时,我运行以下代码将正文呈现为 HTML:
target.body_html = bleach.linkify(bleach.clean(render_template_string(value),tags=allowed_tags, attributes=allowed_attr, strip=True))
render_template_string(value) 是我关心的部分。要在模板字符串(值)中使用宏,每个字符串都必须包含以下内容,以便我在文章正文中使用此文件中的宏:
{% import "includes/macros.html.j2" as macros %}
确保作者可以访问我在他们所有文章中编写的宏是不合理的。有没有办法通过 render_template_string() 函数传递该参数,以便不必在每个字符串中定义它?或者以其他方式创建一个在其中呈现字符串的模板?类似于以下内容:
render_template_string(string,macros=function_to_import_macros("includes/macros.html.j2"))