实例变量 If Else 条件在 HAML 中添加标签

Instance Variable If Else Condition to Add Tag in HAML

我正在尝试在我的 HAML 视图文件中编写条件语句。如果引用对象有 link,我想做的是在块引用的其余部分顶部添加一个 link_to 标记。如果它为零,则格式化 blockqoute 而不在顶部添加 link_to 标记。这是我当前的代码:

.carousel-inner
          - @quotes.each_with_index do |quote, index|
            .item{ class: ("active" if index == 0)}
              - if quote.link.present?
                = link_to quote.link
                  %blockquote
                    .row
                      .col-sm-3.text-center
                        %img.img-circle{:src => quote.avatar, :style => "width: 100px;height:100px;"}
                      .col-sm-9
                        %p= quote.quote
                        %small= quote.author
              - else
                %blockquote
                  .row
                    .col-sm-3.text-center
                      %img.img-circle{:src => quote.avatar, :style => "width: 100px;height:100px;"}
                    .col-sm-9
                      %p= quote.quote
                      %small= quote.author

我得到的当前堆栈错误是:

_quotes.html.haml:26: syntax error, unexpected keyword_else, expecting keyword_end _quotes.html.haml:39: syntax error, unexpected keyword_ensure, expecting end-of-input

任何人 运行 以前都喜欢这样的事情吗?谢谢!

您遗漏了行 = link_to quote.link 中的 do,它会导致结束块的解析失败。您只需要将其更改为:

= link_to quote.link do
  %blockquote
    -# ...