Re: beginner - What is a good way to implement a sub-layout?

70 views
Skip to first unread message

Tom Kenny

unread,
May 13, 2013, 10:46:25 PM5/13/13
to na...@googlegroups.com
Sure: http://nanoc.ws/docs/basics/#as-partials

There's a few ways you can go about it: you can insert the 'only include on /about pages' logic into the layout file, where you'll call the partial:

    <% if @item.path =~ %r{^/about/?} %>
        <%= render 'partials/about-nav' %>    
    <% end %>

That will match against the exact path '/about', too. Note that if you just used

    render 'about-nav' 

that would work, too: the 'render' method uses your Layouts folder as the root. It's a bit cleaner, though, to separate partials from layouts by putting them in /layouts/partials.

Note you can include this in your content files, too, but obviously that wouldn't be DRY and would make no sense. Generally, you want to try and isolate your ERB logic as much as possible, or your markup can turn hideous pretty fast - in this case it would probably be better to move the 'about pages only' check into the partial 'about-nav.html' itself. 

This is only one of a few ways of limiting this partial to only render on a subset of pages within one layout - you could also filter by item properties, eg

     render 'partials/about-nav' if @item[:sidebar_nav] && @item[:sidebar_nav].include?('about')

Which could account for more than one nav, all of which could be individually toggled, like (in the YAML front-matter / header / whatever you call the metadata at the head of each item):

    ---
    title: A note on my gigantic forehead and how to avoid drawing attention to it
    sidebar_nav: [about, forehead_discretion]
    ---

For instance, you might have the different permutations within the one partial file 'sidebar-navs', so from the perspective of the layout or content file, you're just saying 'if there are any sidebar navs, they'll go here'.) You could programatically add 'about' to the sidebar_nav array for all items that match '/about/', as above.

Tom Kenny

unread,
May 13, 2013, 10:49:09 PM5/13/13
to na...@googlegroups.com
Sorry, if I can make an amendment you shouldn't include the '@' sigil before 'item'.

Tom Kenny

unread,
May 13, 2013, 10:52:41 PM5/13/13
to na...@googlegroups.com
God, Google Groups sucks. No Markdown, no previews...

anyway, the full documentation for the #render method is here http://nanoc.ws/docs/api/Nanoc/Helpers/Rendering.html
Reply all
Reply to author
Forward
0 new messages