【问题标题】:How to use a jinja variable inside of a quoted Html attribute?如何在引用的 Html 属性中使用 jinja 变量?
【发布时间】:2023-01-31 08:26:00
【问题描述】:

我想遍历事件和条目,替换 jinja 模板内 html 表单字段中的 id="" 和 name='' 属性中的索引。

是这样的:

{% for e in events %} 
    <td><input id="events-{{ index1 }}-entries-{{ index2 }}" 
        name="events-{{ index1 }}-entries" type="checkbox" 
        value="1"></td>
{% endfor %}

我不知道如何转义属性的引号,以便将变量插入到引用的文本中。

我试图逃避引号,但没有成功。

【问题讨论】:

    标签: python jinja2 flask-wtforms


    【解决方案1】:

    我相信您可以尝试使用字符串连接并将“events-”和“-entries”放在大括号内。例如:

    {{ "Hello " ~ name ~ "!" }} 当将 name 分配给字符串 'John' 时,将返回“Hello John!”

    您可以在 official documentation. 阅读更多关于 Jinja 字符串连接的信息

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 2018-10-13
      • 1970-01-01
      • 2017-05-19
      • 2017-04-10
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多