0

我想对 Slim 使用完整的 html 语法。我不想要他们提供的密码学。但我认为这是不可能的。这是我的 html 代码,Slim 会抛出错误。

目标:

div如果满足某些条件,我不想输出额外的元素类:

<div class="foo bar"></div>   # <-- standard
<div class="foo"></div>   # <-- no "bar" class if some condition is met

这是我的 Slim 代码:

<body> # <-- 11th line
    - (1..5).each do |i|   # <-- The dash means a line of Ruby code...
        <div class="foo    # <-- ...after it an indentation of lines follows
           - if(i != 2)    #  <-- if "i" is not 2
                = bar         # <-- output "bar" as additional class for the div
            = " data-co="#{i}">  # <-- indentation back, I want an output of the rest 
      </div>
</body> # <-- 17th line

</html> # <-- 19th line, the last element

逻辑上一切都很好。遵守所有缩进和=和。-但我得到错误:

!! Unexpected error while processing request: tmpl.slim:16: syntax error, unexpected keyword_end, expecting ')
'
; end; _buf << ("</div>");
     ^
tmpl.slim:17: syntax error, unexpected keyword_end, expecting ')'
; end; _buf << ("</body>"\
     ^
tmpl.slim:22: syntax error, unexpected keyword_end, expecting ')'
end;end;end;end

我有两个问题:

1) 是否可以在 Slim 中完全使用 html 语法?

2) 我该如何解决我的问题?

4

1 回答 1

1

I think you are thinking this the wrong way. If your end goal is to produce two types of opening tags based on one condition, then a simple if else statement will work, which is sort-of what you did. But, because you were writing in multiple lines in an HTML opening tag, SLIM freaked out. Try this out and let me know what happens.

<body>
  - (1..5).each do |i|   
     - if i.eql? 2
       <div class="foo"> 
     - else
       <div class="foo bar" data-co="#{i}">   
     </div>
</body>
于 2013-04-07T21:46:15.110 回答