修剪模板:无法渲染模板,导致语法错误

时间:2013-04-07 21:37:11

标签: ruby template-engine slim-lang

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

目标:

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

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

这是我的修身代码:

<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)我如何解决我的问题?

1 个答案:

答案 0 :(得分:1)

我认为你认为这是错误的方式。如果您的最终目标是根据一个条件生成两种类型的开始标记,那么一个简单的if else语句将起作用,这与您所做的类似。但是,因为你在HTML开头标签中写了多行,所以SLIM吓坏了。试试这个,让我知道会发生什么。

<body>
  - (1..5).each do |i|   
     - if i.eql? 2
       <div class="foo"> 
     - else
       <div class="foo bar" data-co="#{i}">   
     </div>
</body>