代码之家  ›  专栏  ›  技术社区  ›  Sebastian Woinar

Rails模板呈现ERB if语句未按顺序呈现

  •  3
  • Sebastian Woinar  · 技术社区  · 11 年前

    当我在erb-template文件中使用if语句时,if语句的求值会延迟,这会混淆html:

    <small>
    <% if @applause_count == 1 %>
        The author has been cheered up once!
    <% elsif @applause_count > 1%> 
        The author has been cheered up <%=  @applause_count %> times! <br/>Be the next!
    <% end if %> 
    </small>
    

    生产:

    <small>
    </small>
    The author has been cheered up 100 times! <br/>Be the next!
    

    有人能给我解释一下这种奇怪的行为吗?

    1 回复  |  直到 11 年前
        1
  •  2
  •   toms    11 年前

    如前所述,问题在于 <% end if %>

    使用 <% end %>

    这将生成所需的html:

    <small>
    The author has been cheered up 2 times! <br/>Be the next!
    </small>