在我的情况下,所有字符串都是单字的,需要删除字符串中的尾随空格,例如, 'hello '→ 'hello'; 'hello '→ 'hello'。
一种方法可以采用分体式,即,
{% if ' ' in word %}
{% set word = word.split(' ')[0] %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为字符串是一个词。我很确定一定有更好的方法。
jinja2 ×1