1

我的规则文件中有这个:

compile '/gallery/' do  
  filter :haml
  layout :'gallery'
end  
...
compile '/' do  
  filter :haml
  layout :'default'
end  
...
route '/gallery/' do  
  nil
end
...
route '*' do  
  item.identifier.chop + '.' +item[:extension]
end

,所以现在我的 content/index.html 通过 haml 过滤器并编译为 output/index.html 一切都很好。我的 content/gallery.html 包含此代码,该代码也通过了 haml:

#gallery-container  

%ul.items-small  
  %li.item  
    - @item.children.each do |img|  
      %a{:href => "#{img.path}"}  

%ul.items-big  
  %li.item-big  
    - @item.children.each do |img|  
      %a{:href => "#"}  
        %figure  
          %img{:src => "#{img.path(:rep => :thumbnail)}"}  
          %figcaption.img-caption Caption  

,它在文件夹中收集一些图像,content/gallery/当我将路由设置为output/gallery/index.html(查看预览输出吐出)时,我确实得到了我想要的,所以一切都很好。

但是现在我想将生成的代码用作我的部分代码content/index.html,但是当我尝试包含它时,就像=render 'gallery'我没有得到预期的代码一样。反过来我收到错误消息LocalJumpError: no block given (yield)

我的文件中应该有什么layouts/gallery.html?,如果我放在那里,<%= yield %>我会收到上述错误,如果我删除=render 'gallery'没有错误,
但是如果我在我的 index.html 中输入一些文本layouts/gallery.html并再次=render 'gallery'在我的 index.html 中得到该文本layouts/gallery.html,所以它被包括在内并且没有错误。那么我应该 <%= yield %> 我期待的画廊代码layouts/gallery.html然后从 index.html 调用 =render 'gallery' 吗?但这不起作用。此外,它layouts/default.html已经有自己的收益正在工作,然后我尝试在将通过该收益编译的项目中使用该 =render。我做错了吗?我搞不清楚了!

我所有的布局文件都被:erb过滤了。

所以我的问题是如何包含这个部分。谢谢!

4

2 回答 2

1

经过一些尝试和错误后,似乎应该这样做,

= items["/gallery/"].compiled_content

,如果我把它放到我的content/index.html.haml文件中,我会得到我期望的结果,并且我content/gallery.html.haml会在那个地方呈现自己。我还没有确定,我将在哪里使用renderpartials,使用它和我在这里使用的这个有什么区别。

于 2014-01-03T09:38:55.573 回答
1

在挖掘代码后,我找到了另一个解决方案。

render 方法是 的一部分Helpers::Rendering,如果您查看源代码,您会看到它filter_for_layout从规则 ( github ) 调用。

因此,您只需将以下内容添加到您的Rules文件中:

layout '/gallery/', :haml, encoding: 'utf-8'

这样,在呈现画廊布局时,它将像其他布局一样通过 haml 过滤器。

于 2016-12-19T13:26:54.050 回答