如何停止渲染页面/部分

时间:2013-12-11 15:58:21

标签: ruby-on-rails haml

我感兴趣的是我可以在模板中间插入模板渲染吗?例如:

项/ index.html.haml

%h2 Items

-if @items.empty?
  %h3 There are no items
  /X statement/ 


-@items.each do |item|
  /items rendering/ 

因此,如果没有项目,将显示消息并且页面呈现将被中断,否则将呈现项目列表。我现在唯一能做的就是抛出if-else语句。我尝试使用返回代替 X声明,但似乎不能像我期望的那样工作

2 个答案:

答案 0 :(得分:5)

实现该结果的方法正是使用if-else语句。

我不熟悉Haml,但使用好的“旧”ERB的逻辑是

<% if @items.empty? %>
  There are no items
<% else %>
  <% @items.each do |item| %>
  ...
  <% end %>    
<% end %>    

如果您希望拆分条件

,可以使用双精度if
<% if @items.empty? %>
  There are no items
<% end %>    

<% @items.each do |item| %>
...
<% end unless @items.empty? %>    

答案 1 :(得分:3)

基本上,你不能这样做。您可以做的是在开始渲染项目索引之前检查@items是否为空:

- if @items.empty?
  %h3 There are no items
- else
  %h2 Items
  - @items.each do |item|
    /items rendering/
相关问题