【问题标题】:include partial in nanoc [haml]在 nanoc [haml] 中包含部分内容
【发布时间】:2014-01-19 10:09:39
【问题描述】:

我的规则文件中有这个:

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' 没有错误,
但是,如果我在layouts/gallery.html 中添加一些文本,并在我的 index.html 中再次添加 =render 'gallery',我会在 layouts/gallery.html 中得到该文本,因此它会被包含在内并且不会出现错误。那么我应该 我期待的画廊代码到layouts/gallery.html 然后从 index.html 调用 =render 'gallery' 吗?但这不起作用。此外,layouts/default.html 已经有自己的收益正在工作,然后我尝试在将通过该收益编译的项目中使用该 =render。我做错了吗?我迷路了!

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

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

【问题讨论】:

    标签: layout haml render partial nanoc


    【解决方案1】:

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

    = items["/gallery/"].compiled_content
    

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

    【讨论】:

    • 我怀疑渲染仅适用于编译站点时存在的文件。这就是为什么您需要为此使用 nanocs 机制。
    • 希望有一天这会为我清除 :),谢谢您的评论。
    【解决方案2】:

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

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

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

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

    这样,当画廊布局被渲染时,它将像其他布局一样通过 haml 过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多