【问题标题】:Syntax error for if-else statement in html.haml filehtml.haml 文件中 if-else 语句的语法错误
【发布时间】:2023-07-14 15:59:01
【问题描述】:

我正在使用 haml 来编写以下代码。我的 index.html.haml 文件中基本上有一堆嵌套的 if-else 语句。但是,我不断收到以下代码的语法错误。

- if current_user
  :javascript
    window.is_logined_in = true;
    window.currentUserJSON = #{@current_user_json};
    //Getting syntax error for the line below
    - if window.currentUserJSON.user.following_count == 293 && window.currentUserJSON.user.answer_count == 276
      window.complete_orientation = true;
    - else
      window.complete_orientation = false;
- else
  :javascript
    window.is_logined_in = false;

如何修改我的代码来解决这个语法错误?

【问题讨论】:

    标签: jquery html if-statement haml syntax-error


    【解决方案1】:

    您不能像这样在:javascript 块内使用- 前缀。 您实际上是在将代码写入 javascript,这导致了错误, 但是您应该可以在没有前缀 - 的情况下执行此操作,因为它已经是 javascript。

    - if current_user
      :javascript
        window.is_logined_in = true;
        window.currentUserJSON = #{@current_user_json};
        //Getting syntax error for the line below
        if (window.currentUserJSON.user.following_count == 293 
            && 
            window.currentUserJSON.user.answer_count == 276
        ) {
          window.complete_orientation = true;
        } else {
          window.complete_orientation = false;
        }
    - else
      :javascript
        window.is_logined_in = false;
    

    javascript 代码本身有点冗长,但它应该可以完成这项工作。

    【讨论】:

      最近更新 更多