rubynanoc

nanoc and multiple layouts


is it possible to use multiple layouts for a specific (or all) item(s)? For example, I have a couple of items, and I want to apply two different layouts to it. One with a green and one with a blue background (however). And I want to compile them in two different folders in my output directory (e.g. v1 and v2).

I was playing with the rules and the compile blocks, but I couldn't figure out how this could work. Because, every item gets compiled only one time during the compile process, I can't tell nanoc to compile it the first time with layout1 and the second time with layout2. I tried sth like this, but it led to broken output files.

compile '*' do
  if item.binary?
    # don’t filter binary items
  else
    filter :erb
    layout 'layout1'
    layout 'layout2'
  end
end

Hope I made myself clear and somebody can help.

thx, tux


Solution

  • Item representations are meant for this purpose. You can create two different representations, e.g. the default one and an alternative one, and then apply compilation and routing rules to them, like this:

    # default rep, although you can pass
    # :rep => :default explicitly too
    compile '/stuff/*/' do
      filter :erb
      layout 'default'
    end
    
    route '/stuff/*/' do
      # /stuff/foo/ -> /boring/stuff/foo/
      # Just an example; you probably need something else
      '/boring' + item.identifier
    end
    
    compile '/stuff/*/', :rep => :special do
      filter :erb
      layout 'special' # this is different
    end
    
    route '/stuff/*/', :rep => :special do
      # /stuff/foo/ -> /special/stuff/foo/
      # Again, just an example
      '/special' + item.identifier
    end