Liquid syntax error: Tag was not properly terminated with regexp: /\%\}/

Liquid syntax error: Tag was not properly terminated with regexp: /\%\}/

我的液体模板中有一个 if 条件,就像这样

Username: {{user.email}}
{% if extras['password_to_be_sent'] %}
Password: {{extras['password_to_be_sent']}}
{% endif %}
Trial expiration: {{user.trial_expiration}}

然而,当 if 条件未计算为 true

时,这会生成一个换行符

所以上面生成的输出是这样的

Username: Abc

Trail Expiration: 2019-11-10

我想在 if 条件未计算为 true

时删除额外的换行符

我尝试按照此处的建议添加 - https://shopify.github.io/liquid/basics/whitespace/

因此将代码更新为

Username: {{user.email}}
{%- if extras['password_to_be_sent'] -%}
Password: {{extras['password_to_be_sent']}}
{%- endif -%}
Trial expiration: {{user.trial_expiration}}

但这给出了一个例外 Liquid::SyntaxError (Liquid syntax error: Tag '{%- if extras['password_to_be_sent'] -%}' was not properly terminated with regexp: /\%\}/)

此外,如果有帮助,我会将模板代码保存在数据库中。

如有任何帮助,我们将不胜感激。谢谢。

您是否检查过以确保 extras['password_to_be_sent'] 不是 return 真实但空的字符串?也许应该是:

Username: {{user.email}}
{%- if extras['password_to_be_sent'].present? -%}
Password: {{extras['password_to_be_sent']}}
{%- endif -%}
Trial expiration: {{user.trial_expiration}}

上面在评论中回答了,但这里又是:

I see you've also tagged Ruby on Rails — if your Rails app is consuming a version of Liquid earlier than 4.0.0, you won't be able to use the whitespace control tags. Those were added in Liquid 4.0.0.

当我从

更改第 134 行时
int dis[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

int dis[4][2]={ {0,1},{0,-1},{1,0},{-1,0}};

已修复!