【问题标题】:Liquid syntax error: Tag was not properly terminated with regexp: /\%\}/Liquid 语法错误:标签未使用正则表达式正确终止:/\%\}/
【发布时间】: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


【解决方案1】:

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

我看到您还标记了 Ruby on Rails — 如果您的 Rails 应用程序使用低于 4.0.0 的 Liquid 版本,您将无法使用空白控制标记。这些是在 Liquid 4.0.0 中添加的。

【讨论】:

    【解决方案2】:

    您是否检查以确保extras['password_to_be_sent'] 没有返回真实但空的字符串?也许应该是:

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

    【讨论】:

    • .present? 不是有效的 Liquid。您可以通过将相等性与特殊的 blank 关键字进行比较来做类似的事情:{% if foo == blank %} This will only print if foo is not set or if it's an empty string {% endif %}
    【解决方案3】:

    当我更改第 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}};
    

    解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多