无法在 Slim 逻辑中迭代数组

发布于 2024-12-18 13:16:32 字数 686 浏览 3 评论 0原文

我正在尝试迭代名为 @items 的数组。我认为通过将其添加到类变量并从 SLIM 调用它,我也许能够输出该数组。我做错了什么?

require "rubygems"
require "sinatra"
require "slim"
set :port, 80
get "/" do
  slim :index
end
get "/history" do
  @items = Dir.entries(File.expand_path(File.dirname(__FILE__)) + "/history").to_a
  slim :history
end
__END__
@@layout
doctype html
html
  head
    title Web View
  body
    div id="main" name="main"
      h1 Web View
      == yield
@@index
h2 Available Options
a href="/history" id="history_view" name="history_view" History
@@history
h2 History
- unless items.empty?
  - for item in items do
    a href="#{item}/output.html" item
  - else
    p NO ITEMS FOUND

I am trying to iterate through an array named @items. I thought by adding it to a class variable and calling it from SLIM I might be able to output the array. What am I doing wrong?

require "rubygems"
require "sinatra"
require "slim"
set :port, 80
get "/" do
  slim :index
end
get "/history" do
  @items = Dir.entries(File.expand_path(File.dirname(__FILE__)) + "/history").to_a
  slim :history
end
__END__
@@layout
doctype html
html
  head
    title Web View
  body
    div id="main" name="main"
      h1 Web View
      == yield
@@index
h2 Available Options
a href="/history" id="history_view" name="history_view" History
@@history
h2 History
- unless items.empty?
  - for item in items do
    a href="#{item}/output.html" item
  - else
    p NO ITEMS FOUND

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-12-25 13:16:32

您似乎尝试在“历史记录”布局中调用 items 方法。除此之外,请查看 else 子句的缩进级别。尝试更改您的代码:

h2 History
- unless @items.empty?
  - for item in @items do
    a href="#{item}/output.html" item
- else
    p NO ITEMS FOUND

It seems that you've tried to call items method in your 'history' layout. Besides this look at indentation level for your else clause. Try to change your code:

h2 History
- unless @items.empty?
  - for item in @items do
    a href="#{item}/output.html" item
- else
    p NO ITEMS FOUND
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文