在 Slim 中比较数字

Comparing numbers in Slim

在 Slim 中,如何比较数字?

我想要类似的东西:

- if match_percent > 50
  span.classOne
  | #{match_percent} %
- else
  span.classTwo
  | #{match_percent} %

但是“>”被认为是语法错误。这可能吗? Slim 的文档绝对糟糕。我在任何地方都找不到关于此的任何信息

没有看到你的回溯错误,这很难调试,但它应该可以工作。

test.slim

doctype html
html
  head
    title Slim Examples
    meta name="keywords" content="template language"
    javascript:
      alert('Slim supports embedded javascript!')

  body
    h1 Markup examples

    #content
      p This example shows you how a basic Slim file looks.

    - if 1 < 2 
        p 1 is less than 2 
    - else
      p No items found. Please add some inventory.
        Thank you!

    div id="footer"
      | Copyright &copy; #{`Time`} #{'me'}

运行 这来自命令行,没有错误

slimrb test.slim
#=>
<!DOCTYPE html><html><head><title>Slim Examples</title><meta 
content="template language" name="keywords" /><script>alert('Slim 
supports embedded javascript!')</script></head><body><h1>Markup 
examples</h1><div id="content"><p>This example shows you how a basic 
Slim file looks.</p></div><p>1 is less than 2 </p><div 
id="footer">Copyright &copy;  me</div></body></html>

更新

根据您上面的评论:

undefined method `>' for nil:NilClass

意味着在 ruby 中 > 左侧调用的任何内容都返回 nil。所以你的错误就是这样。因此,在您的示例中,您可以使用字符串插值来解决此问题。

- if "#{match_percent.to_i}" > 50
  span.classOne
  | #{match_percent} %
- else
  span.classTwo
  | #{match_percent} %

这样,如果 match_percent 为零,您的 if 语句仍然有效。