【问题标题】:Jinja nested template inside variable in templateJinja 在模板中的变量内嵌套模板
【发布时间】:2021-07-27 12:54:37
【问题描述】:

我想做自定义通知系统。现在我在数据库中有表,有 html 代码,我放在我的页面上。 但是如果我在这段代码中插入 jinja 表达式,它们就会显示为纯文本。

例子:

def render():
    hidden_variable = "world"
    example = "Hello {{hidden_variable}}!"

    return {'example': example, 'hidden_variable': hidden_variable}

模板.html

<div>
{{example}}
</div>

我得到了什么:

Hello {{hidden_variable}}!

我想要什么:

Hello world!

【问题讨论】:

标签: python jinja2


【解决方案1】:

那个 example 被存储为字符串,因为您在 python 函数中使用 jinja 语法。

Jinja 是 Python 的 Web 模板引擎。

改为:

example = f"Hello {hidden_variable}!"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多