【发布时间】:2019-10-30 15:30:08
【问题描述】:
我的液体模板中有一个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: /\%\}/)
另外,如果有帮助,我会将模板代码保存在数据库中。
对此的任何帮助将不胜感激。谢谢。
【问题讨论】:
-
您好!您的应用使用的是什么版本的 Liquid?我看到您还标记了 Ruby on Rails,因此如果您的 Rails 应用程序使用 4.0.0 之前的 Liquid 版本,您将无法使用空白控制标记。这些是在 Liquid 4.0.0 中添加的。
-
嗨@AdamHollett,感谢您的回复。我使用的是 gem 版本 3.0.6,我将其更新为 4.0.3,但现在我的 user.rb 中出现错误
undefined method liquid_methods -
嗨@AdamHollett,这就是问题所在,你想把这个放在答案中吗,所以如果有人访问这个帖子会有帮助,我可以将答案标记为已接受,谢谢
-
完成!感谢您回复我。
标签: ruby ruby-on-rails-5 liquid