【问题标题】:Slim templates: Can't render a template, get syntax errorSlim 模板:无法渲染模板,出现语法错误
【发布时间】:2013-04-07 21:37:10
【问题描述】:

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

【问题讨论】:

  • 第 16 行是哪一行?

标签: ruby template-engine slim-lang


【解决方案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>

【讨论】:

  • 是的,这行得通! :) 我现在看到了区别。我想只添加类名而不重复 &lt;div&gt;...&lt;/div&gt; 行。现在我知道如何解决它了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-01-24
  • 2016-02-26
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多